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.

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

101. Symmetric Tree(判断二叉树是否对称)的更多相关文章

  1. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

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

  2. 101. Symmetric Tree -- 判断树结构是否对称

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

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

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

  4. 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的.    1   / \  2   2 / \ / \3  4 4  3但是 ...

  5. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  6. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

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

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

  8. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  9. <LeetCode OJ> 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  10. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

随机推荐

  1. CListBox自动换行显示

    需要在ListBox控件中显示一些信息.为方便查看,不使用水平滚动条.当要输出的字符串占用的宽度超过ListBox的宽度时,截断字符串,剩余的在下一行显示. 1. 计算ListBox所占的宽度,用Ge ...

  2. OEM无法启动:oracle/product/10.2.0/dbhome_1/oc4j/j2ee/OC4J_DBConsole_localhost.localdomain_orcl not found.

    配置OEM环境变量 ORACLE_UNQNAME=$ORACLE_SID 要想打开OEM的web管理程序,必须先启动Database listener,再启动Datebase Control端口监控程 ...

  3. uva 11381(神奇的构图、最小费用最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=29821 思路:首先拆点,每个字符对应的位置拆成i和i+len,然后 ...

  4. spring配置文件头部配置解析

    http://blog.csdn.net/f_639584391/article/details/50167321

  5. 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题

    一.ASP.NET MVC中使用OutputCache实现服务器端页面级缓存 在ASP.NET MVC中,假如我们想要将某个页面(即某个Action)缓存在服务器端,可以在Action上标上以下特性: ...

  6. Excel宏被禁用解决办法

  7. 深入理解ByteBuffer

    ByteBuffer类是在Java NIO中常常使用的一个缓冲区类,使用它可以进行高效的IO操作,但是,如果对常用方法的理解有错误,那么就会出现意想不到的bug. ByteBuffer类的常用方法 先 ...

  8. PHP mysql基本语句指令

    /*选择数据库 use test; */ /* 显示所有的数据库 show databases; */ /*删除表/数据库 drop database test1; delete from user1 ...

  9. [ Office 365 开发系列 ] 身份认证

    前言 本文完全原创,转载请说明出处,希望对大家有用. 通常我们在开发一个应用时,需要考虑用户身份认证及授权,Office 365使用AAD(Azure Active Directory)作为其认证机构 ...

  10. Zabbix使用SMTP发送邮件报警并且制定报警内容

    接上篇Zabbix监控介绍及安装配置 选择报警项 创建一个报警项 选择到刚刚自定义的80端口 定义报警方法 定义告警级别 一些报警方法 diff 比较是否有修改 last 最低值 nodata 没有数 ...