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对称树的更多相关文章

  1. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  2. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  3. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  4. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  5. LeetCode 101. Symmetric Tree (对称树)

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

  6. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  7. LeetCode 101. Symmetric Tree(镜像树)

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

  8. 【LeetCode】Symmetric Tree(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...

  9. leetcode 101 Symmetric Tree ----- java

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

随机推荐

  1. Django 数据库的迁移

    先数据库迁移的两大命令: python manage.py makemigrations & python manage.py migrate 前者是将model层转为迁移文件migratio ...

  2. 尚硅谷springboot学习12-profile

    一个项目对应不同的环境可以会有不同的配置,如开发,测试,生产环境使用不同的端口,这时可以设置profile变换不同的环境 通过spring.profiles.active切换环境 1.多Profile ...

  3. C语言基础入门

    搭建Windows平台C/C++开发环境 第1步: 第2步: 第3步: 第4步: 第5步: 第6步: 第7步: 第8步: 第9步: 第10步: 第11步: 第12步: 第13步: 第14步: 第15步 ...

  4. linux mce的一些相关内容和用户态监控的设计方法

    之所以想起写一点关于mce的东西,倒不是因为遇到mce的异常了,之前遇到过很多mce的异常,内存居多,但没有好好记录下来,写这个是因为参加2018 clk南京会议的一点想法. void __init ...

  5. js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)【转载】

    我们或多或少都使用过各式各样的富文本编辑器,其中有一个很方便功能,复制一张图片然后粘贴进文本框,这张图片就被上传了,那么这个方便的功能是如何实现的呢? 原理分析 提取操作:复制=>粘贴=> ...

  6. ImportError: No module named etree.ElementTree问题解决方法

    学习python操作xml文档过程中碰到的ImportError: No module named etree.ElementTree问题,问题现象比较奇怪,做个记录. 操作环境 Python3.6+ ...

  7. Eclipse编译Android项目时出现的问题:Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead.

    Consle: Android requires compiler compliance level 5.0 or 6.0. Found '1.8' instead. Please use Andro ...

  8. asp.net中的reportview报错跟预编有关系

    当报表控件出现: 报表定义无效.详细信息:根级别上的数据无效.行1,位置1. 先检查一下,你的aspx文件是不是变成了这样一句话 这是预编译工具生成的标记文件,不应被删除! 如果这样的话,报表控件是不 ...

  9. CentOS下mysql数据库常用命令

    1.更改root密码 mysqladmin -uroot password 'yourpassword' 2.远程登陆mysql服务器 mysql -uroot -p -h192.168.137.10 ...

  10. CSS----盒子模型与浮动

     盒模型(框模型) 页面上任何一个元素我们都可以看成是一个盒子,盒子会占用一定的空间和位置他们之间相互制约,就形成了网页的布局 w3c的盒模型的构成:content border padding ma ...