问题描述:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
解题思路:
对称二叉树,同等价二叉树思路一样,直接给出左左子树与右右子树进行比较是否等价。
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
//else if(!root->left&&!root->right) return true;
else return sureSymmetric(root->left,root->right);
}
bool sureSymmetric(TreeNode* left,TreeNode* right)
{
if(left==NULL&&right==NULL)
return true;
else if((left!=NULL&&right==NULL)||(left==NULL&&right!=NULL)||left->val!=right->val)
return false;
else return sureSymmetric(left->left,right->right)&&sureSymmetric(left->right,right->left);
}
};