C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if ( !root ) {
return true;
}
return isMiror(root -> left, root -> right);
}
bool isMiror(TreeNode* left, TreeNode* right) {
if ( !left && !right) {
return true;
}
if ( !left || !right ) {
return false;
}
if ( left -> val != right -> val ) {
return false;
}
if (!isMiror(left -> left, right -> right)) {
return false;
}
return isMiror(left -> right, right -> left);
}
};
网友评论