[leetcode]101. Symmetric Tree对称树
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
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题目
给定二叉树,判断其是否左右对称
思路
DFS
代码
/*
Time:O(n),
Space: O(logn) 即 O(h) 树的deepest hight
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return isSymmetric(root.left, root.right);
}
private static boolean isSymmetric(TreeNode p, TreeNode q) {
if (p == null && q == null) return true; // 终止条件
if (p == null || q == null) return false; // 终止条件
return p.val == q.val // 三方合并
&& isSymmetric(p.left, q.right)
&& isSymmetric(p.right, q.left);
}
}
[leetcode]101. Symmetric Tree对称树的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- SpringBoot配置定时任务的两种方式
一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- 跟我一起学Python-day1(条件语句以及初识变量)
通过练习题来学习条件语句 1,使用while循环输出1 2 3 4 5 6 8 9 10 n=1 while n<11: if n=7: pass else: print(n) n=n ...
- Linux:条件变量
条件变量: 条件变量本身不是锁!但它也可以造成线程阻塞.通常与互斥锁配合使用.给多线程提供一个会合的场所. 主要应用函数: pthread_cond_init函数 pthrea ...
- Android 操作UI线程的一些方法
我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...
- 命令行下IIS的配置脚本Adsutil.vbs
命令行下IIS的配置脚本Adsutil.vbs 2009-08-20 12:26:52 www.hackbase.com 来源:Jackal's Blog Jackal's Blog文件存在于:C ...
- http://wenku.baidu.com/view/26afdb8371fe910ef12df8ccRevit采用DWG和FBX两种格式导入3D max方法的总结
2.DWG 属性——导出为 ACIS 实体(如果选择导出为多边形网格,则将每个图元导出为 由多个多边形组成的对象,这些多边形相互连接或组成 “ 网格 ” .在导出到 DWG 文件以用 于 Ma ...
- C# 指定平台编译项目
如CefSharp就需要指定平台,项目为Any CPU时,无法编译,总会提示出错. 如: CefSharp.Common contains unmanaged resoures, set your p ...
- python遇到的错误
今天学习文件遇到这个错误. 这是在 text_files\vvvv.txt 之间加一个\ 就可以了,变成 text_files\\vvvv.txt,运行成功
- 媒体类型 & 媒体查询
[媒体类型 & 媒体查询] @media 规则允许在相同样式表为不同媒体设置不同的样式. 在下面的例子告诉我们浏览器屏幕上显示一个14像素的Verdana字体样式.但是如果页面打印,将是10个 ...
- 解题9(StringReversedOrder)
题目描述 将一个英文语句以单词为单位逆序排放.例如“I am a boy”,逆序排放后为“boy a am I”所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符 接口说明 /** * ...