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

这题注意不能判断左右孩子的value是不是一致,那不是对称;而是要判断recursion(left.right, right.left) && recursion(left.left, right.right,所以要新开一个递归函数。

这递归里又有技巧。

重点1 :技巧

 if (left == null || right == null) return left == right;
复制代码

这一行就抵了下面两行:

        if (left == null && right == null) return true;if (left == null || right == null) return false;
复制代码

重点2:递归的终止条件应该是返回false(除非判断不需要递归了,否则不能阻止进入递归)

递归里的条件,应该是判断什么时候终止,也就是返回false。不能在进入递归之前就结束了。比如下面这句:

        if (left.val != right.val) return false;复制代码

我一开始写的是:

        if (left.val == right.val) return true;复制代码

这种的话遇到

[1,2,2,null,3,null,3]

这棵树就不行了,我想了半天没想通,只好debug了一下。。结果发现自己太蠢:如果写if (left.val == right.val) return true;,那走到第二层的时候直接就返回true了,根本没进入递归啊。所以递归的终止条件应该是返回false。不能在进入递归之前就结束了。

就是说,除非判断不需要递归了,否则不能阻止进入递归。 所以, if (left == null || right == null) return left == right;这种就没有问题,因为它不需要继续递归了。

    public boolean isSymmetric(TreeNode root) {if (root == null) return true;return recursion(root.left, root.right);}private boolean recursion(TreeNode left, TreeNode right) {if (left == null || right == null) return left == right;if (left.val != right.val) return false;return recursion(left.right, right.left) && recursion(left.left, right.right);}
复制代码

刚直播覃超把代码修改了一下,他说if (left.val != right.val) return false;这种是不好的,但是直接return的话又不好因为后面还有一个return。 所以他改成了这样:

        return left.val == right.val && recursion(left.right, right.left) && recursion(left.left, right.right);
复制代码

转载于:https://juejin.im/post/5a31340ff265da43333e6761

101 Symmetric Tree相关推荐

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

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

  2. LeetCode 101. Symmetric Tree

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

  3. 101. Symmetric Tree (C语言)

    101. Symmetric Tree (C语言) 判断是否为左右镜面对称的二叉树 题目 Given the root of a binary tree, check whether it is a ...

  4. Leetcode: 101. Symmetric Tree

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

  5. Leet Code OJ 101. Symmetric Tree [Difficulty: Easy]

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

  6. leetcode python3 简单题101. Symmetric Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...

  7. 【easy】101. Symmetric Tree

    判断一棵二叉树是否对称 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* T ...

  8. leetcode 101 Symmetric Tree

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

  9. LeetCode Algorithm 101. Symmetric Tree

    Title 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1/ \2 2/ \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...

  10. Symmetric Tree (101)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

最新文章

  1. python多次循环输出_函数的Python循环(多次将输出作为输入重用)
  2. 如何在桌面上安装运行Rancher 2.0
  3. 深度学习核心技术精讲100篇(四十一)-阿里飞猪个性化推荐:召回篇
  4. VMware新装CentOS无法连接外网解决方法
  5. php等级证书,php银行开放平台接口1:php 对cer证书处理
  6. 【v8】一些关于内存泄漏的踏坑
  7. cad字体渐变_[AI10]透明渐变得问题 字体命令的一个变 pantone色系 AutoCAD2006中文版...
  8. 语义分割项目:病害程度定量估算(求每类占总量比例)
  9. [转载] python numpy np.exp()函数
  10. MTK ELT工具,驻网
  11. Java项目本地部署宝塔搭建实战-医院HIS系统源码
  12. 程序员也需要掌握的常用Linux网络命令汇总
  13. Xcode真机测试(无需开发者账号)
  14. 没事别想不开去创业公司
  15. android 开机动画实现,Android App启动画面实现
  16. Godot 导出项目文件错误解决办法
  17. 【论文阅读】LOKI-Practical Data Poisoning Attack against Next-Item Recommendation
  18. 通过读取csv/xmladd()reduct()数据并使用allure展示测试报告
  19. localbus总线
  20. 养猪还是养羊,刀杀还是电击?

热门文章

  1. Apache Flink 1.9 版本即将发布,新版本有哪些新特性
  2. 同事乱用 Redis 卡爆,我真是醉了
  3. 毕业前五年,我们应该把钱看轻
  4. java跨平台_Java如何实现跨平台
  5. python创建gui界面_你要的 Python 创建 GUI 用户界面程序,来咯
  6. cb rm –rf_php执行rmrf命令
  7. SpringBoot日志的相关知识
  8. 使用Python的pip方法安装第三方包时,很慢或者失败的问题
  9. springboot与dubbo整合
  10. 深入理解计算机操作系统(六)