Symmetric Tree (101)
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)相关推荐
- 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /** * Definition for a binary tree no ...
- LeetCode 101. Symmetric Tree
LeetCode 101. Symmetric Tree Solution1 参考<剑指offer>上的解法:https://blog.csdn.net/allenlzcoder/arti ...
- 101. Symmetric Tree (C语言)
101. Symmetric Tree (C语言) 判断是否为左右镜面对称的二叉树 题目 Given the root of a binary tree, check whether it is a ...
- 【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 ...
- Leetcode: 101. Symmetric Tree
题目 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo ...
- LeetCode 集锦(二十二) - 第 101 题 Symmetric Tree
问题 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ...
- 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 ...
- leetcode python3 简单题101. Symmetric Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零一题 (1)题目 英文: Given a binary tree, chec ...
- 【easy】101. Symmetric Tree
判断一棵二叉树是否对称 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* T ...
最新文章
- python使用np.argsort对一维numpy概率值数据排序获取升序索引、获取的top索引(例如top2、top5、top10)索引二维numpy数组中对应的原始数据:原始数据概率最小的头部数据
- Tomcat学习总结(2)——Tomcat使用详解
- argument在python里什么意思_python add_argument() 的使用方法
- 多线程c语言,如何用C语言实现多线程
- LwIP Application Developers Manual9---LwIP and multithreading
- 在布局空间标注的尺寸量不对_卫生间最佳布局尺寸,合理布局做到1毫米都不浪费!...
- cookie、session、sessionid 与jsessionid之间的关系
- python2.7.12源码编译
- 学习英语,先记下留着后用
- java多线程-线程的实现方式
- 换服务器原网站数据会丢失吗,ecs服务器更换操作系统后原服务器数据还在吗?...
- 使用正则表达式大写SQL关键字
- 未来的人工智能和 AR/VR 会从哪些方面影响教育?有什么机会?
- 窘境与出路:AI时代的女性科技光芒
- HttpClient忽略SSL证书,附带百度网盘jar包
- 七种方法绕过安卓手机锁屏
- 解决ie浏览器不兼容浮动float的办法
- outlook安全电子邮件实现
- 【题解】LuoGu4158:粉刷匠
- BP神经网络做分类+隐含层节点确定+红酒数据为例