码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 101.对称二叉树

时间:2018-12-23 11:15:18      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:class   family   public   div   int   val   size   amp   span   

对称二叉树

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

例如,二叉树 [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 };

 

Leetcode 101.对称二叉树

标签:class   family   public   div   int   val   size   amp   span   

原文地址:https://www.cnblogs.com/kexinxin/p/10163093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!