Symmetric Tree

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

思路:其中左子树和右子树对称的条件:

  • 两个节点值相等,或者都为空

  • 左节点的左子树和右节点的右子树对称

  • 左节点的右子树和右节点的左子树对称

时间复杂度: 本质其实就是DFS,时间复杂度为O(n)
空间复杂度: O(h)

public class Solution {public boolean isSymmetric(TreeNode root) {if (root == null) {return true;}return helper(root.left, root.right);}private boolean helper(TreeNode left, TreeNode right) {if (left == null && right == null) {return true;}if (left == null || right == null) {return false;}if (left.val != right.val) {return false;} return helper(left.left, right.right) && helper(left.right, right.left);}
}

Symmetric Tree (101)相关推荐

  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从零单排】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 ...

  5. Leetcode: 101. Symmetric Tree

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

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

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

  7. 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 ...

  8. leetcode python3 简单题101. Symmetric Tree

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

  9. 【easy】101. Symmetric Tree

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

最新文章

  1. python使用np.argsort对一维numpy概率值数据排序获取升序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据
  2. Tomcat学习总结(2)——Tomcat使用详解
  3. argument在python里什么意思_python add_argument() 的使用方法
  4. 多线程c语言,如何用C语言实现多线程
  5. LwIP Application Developers Manual9---LwIP and multithreading
  6. 在布局空间标注的尺寸量不对_卫生间最佳布局尺寸,合理布局做到1毫米都不浪费!...
  7. cookie、session、sessionid 与jsessionid之间的关系
  8. python2.7.12源码编译
  9. 学习英语,先记下留着后用
  10. java多线程-线程的实现方式
  11. 换服务器原网站数据会丢失吗,ecs服务器更换操作系统后原服务器数据还在吗?...
  12. 使用正则表达式大写SQL关键字
  13. 未来的人工智能和 AR/VR 会从哪些方面影响教育?有什么机会?
  14. 窘境与出路:AI时代的女性科技光芒
  15. HttpClient忽略SSL证书,附带百度网盘jar包
  16. 七种方法绕过安卓手机锁屏
  17. 解决ie浏览器不兼容浮动float的办法
  18. outlook安全电子邮件实现
  19. 【题解】LuoGu4158:粉刷匠
  20. BP神经网络做分类+隐含层节点确定+红酒数据为例

热门文章

  1. logger异常日志要点总结
  2. 部分 DNS 查询因闰秒 bug 报错
  3. gtest hook 示例
  4. ADO RecondsetPtr 以及如何实现对表的增加删除数据
  5. 转:性能测试中常见的性能问题及识别方法
  6. 有关DataVisualization类组件的研究——Silverlight学习笔记[43]
  7. Netty的ChannelFuture和ChannelPromise
  8. html记仇表情包源码,写小本本记仇表情包
  9. python 碎片图像拼接_python实现图像拼接
  10. 第4章 MySQL数据库结构优化