Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following is not:
1 / \ 2 2 \ \ 3 3
1、空集竟然会返回true
2、貌似这种通过&&向上层传递bool值的方法很常见
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool check(TreeNode *node1,TreeNode *node2)
{
if(node1==NULL&&node2==NULL)
return true;
if(node1==NULL||node2==NULL)
return false;
if(node1->val!=node2->val)
return false;
else
return check(node1->left,node2->right)&&check(node1->right,node2->left);
}
bool isSymmetric(TreeNode *root) {
if(root==NULL)
return true;
return check(root->left,root->right);
}
};