Leetcode 101.对称二叉树

对称二叉树

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

1

/ \

2 2

/ \ / \

3 4 4 3

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

1

/ \

2 2

\ \

3 3

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分。

 1 class Solution{
 2 public:
 3     bool isSymmetric(TreeNode* root){
 4         if(root==NULL) return 1;
 5         return judge(root->left,root->right);
 6     }
 7 
 8     int judge(TreeNode *root1,TreeNode *root2){
 9         if(!root1&&!root2) return 1;
10         else if(root1&&root2&&root1->val==root2->val&&judge(root1->left,root2->right)&&judge(root1->right,root2->left)) return 1;
11         else return 0;
12     }
13 };

猜你喜欢

转载自www.cnblogs.com/kexinxin/p/10163093.html