思路:

主要判断左子树与右子树。

在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较。

到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了。

当某个结点对称了,它的左子树也对称了,右子树也对称了,那才是真的对称了。

C++

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSymmetric(TreeNode *root) {
13         if(!root)
14             return true;    //无根结点
15         else if(root->left==(void*)0 && root->right==(void*)0)        //只有根结点的树
16             return true;
17         else if(root->left!=(void*)0 && root->right!=(void*)0 && isSymmetric(root->left,root->right))    //两个子树非空,左右子树为对称
18             return true;
19         else
20             return false;
21     }
22     bool isSymmetric(TreeNode *left,TreeNode *right) {
23         if(left->val==right->val){        //左右结点值相等
24             if(left->left==(void*)0 && right->right==(void*)0 || left->left!=(void*)0 && right->right!=(void*)0 && isSymmetric(left->left,right->right)){        //左右子树都为空、或者对称
25                 if(left->right==(void*)0 && right->left==(void*)0 ||left->right!=(void*)0 && right->left!=(void*)0 && isSymmetric(left->right,right->left))      //右左子树都为空、或对称
26                     return true;
27                 else
28                     return false;
29             }
30             else    //左右子树不对称
31                 return false;
32         }
33         else //左右结点值不等
34             return false;
35     }
36 };

AC代码

python3:  两种方法

  增加默认参数。

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def isSymmetric(self, root, right=None, isroot=True):
10         """
11         :type root: TreeNode
12         :rtype: bool
13         """
14         F=self.isSymmetric
15         if isroot==True:    #根
16             if root==None:    return True
17             return F(root.left, root.right, False)
18         elif root==None and right==None:    #都是空
19             return True
20         elif root and right and root.val==right.val:    #都不空
21             return F(root.left, right.right, False) and F(root.right, right.left, False)
22         return False

AC代码

  在函数中定义辅助函数。

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def isSymmetric(self, root):
10         """
11         :type root: TreeNode
12         :rtype: bool
13         """
14         def help(L, R):
15             if not L and not R:
16                 return True
17             elif L and R and L.val==R.val:
18                 return help(L.left,R.right) and help(L.right,R.left)
19             else:
20                 return False
21         if root==None:    return True
22         return help(root.left, root.right)

AC代码

转载于:https://www.cnblogs.com/xcw0754/p/4076153.html

LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)相关推荐

  1. LeetCode 101. Symmetric Tree

    LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...

  2. LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历--递归,迭代--反向压栈--C++解法

    LeetCode 589. N-ary Tree Preorder Traversal-多子节点树前序遍历–递归,迭代–反向压栈–C++解法 LeetCode题解专栏:LeetCode题解 LeetC ...

  3. Leetcode: 101. Symmetric Tree

    题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...

  4. LeetCode OJ -- Binary Tree Paths

    http://blog.ubooksapp.com/ 标签(空格分隔): LeetCode OJ BinaryTree Given a binary tree, return all root-to- ...

  5. leetcode 101 Symmetric Tree

    判定两棵树是否严格镜像对称. 解决: BFS bool isSymmetric(TreeNode* root) {if (root == NULL)return true;deque<TreeN ...

  6. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  7. LeetCode 集锦(二十二) - 第 101 题 Symmetric Tree

    问题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ...

  8. LeetCode LCP 07. 传递信息 / NC111 最大数 / NC16 判断二叉树是否对称 / NC13 二叉树的最大深度

    祝我党百年华诞生日快乐 LCP 07. 传递信息 2021.7.1 每日一题 题目描述 小朋友 A 在和 ta 的小伙伴们玩传信息游戏,游戏规则如下:有 n 名玩家,所有玩家编号分别为 0 - n-1 ...

  9. 【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree

    题目 1.same tree Given two binary trees, write a function to check if they are equal or not. Two binar ...

最新文章

  1. ggbiplot-最好看的PCA作图:样品PCA散点+分组椭圆+变量贡献与相关
  2. [01]关于TDD、BDD和DDD的一些看法
  3. python中global的用法
  4. Requires: libstdc++.so.6(GLIBCXX_3.4.15)
  5. 组合数 com(n,r)
  6. 【渝粤教育】国家开放大学2018年秋季 0508-21T影视特技及后期合成 参考试题
  7. C/C++字符串处理库
  8. 单片机定时器_单片机定时器/计数器基本原理
  9. python自动化测试之mysql5.0版本数据库查询数据时出现乱码问题分析
  10. lua语言入门学习(三)lua语言的小demo之游戏2048
  11. python绘制emoji_在Python中将emoji转换为文本
  12. 【20保研】四川大学计算机学院(软件学院)2019年全国优秀大学生暑期夏令营招生简章...
  13. 【软件网每日新闻播报│第9-20期】
  14. 怎么在当前文件目录下打开CMD
  15. window.name属性
  16. [Idea] Add Abbreviation的使用
  17. 微信表情的字符编号完整版【图文并茂哦 】
  18. 关于冒险岛,8090的不二游戏
  19. IIS 服务器常见漏洞
  20. 防火墙、WAF、IPS、IDS、堡垒机的区别

热门文章

  1. 基于AVR单片机PWM功能的数控恒流源研制
  2. python文档字符串格式_Python字符串及文本模式方法详解
  3. php图片提交,php+js实现图片的上传、裁剪、预览、提交示例
  4. python界面设计模块_Python+Pycharm+PyQT5可视化程序设计入门
  5. Mysql 扩展性设计之Replication,在Mysql具有很相当重要的位置,主从、主主从,你了解他们的背后逻辑吗
  6. 如何把电脑文件传到虚拟机
  7. Python字典操作
  8. 搭建hadoop和spark分布式环境
  9. zynq的emio和axi_【ZYNQ7000学习之旅 - 01】EMIO练习
  10. Java如何隐藏控制按键动画_Java动画短片当不移动鼠标光标时