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. git chekout分支遇到问题:need merge

    解决步骤: 在master上, 1.git add . 2.git commit 3.新建分支,并且checkout到此分支,重新提交

  2. VS工程目录下的ipch文件夹和.sdf文件

    Visual Studio 2010工程目录下的ipch文件夹和.sdf文件 - web8 - 博客园http://www.cnblogs.com/web100/archive/2012/12/21/ ...

  3. 指针*pbuffer和getchar 读取字符串

    在C语言入门教材里看到这一段代码,没看懂是什么意思.char buffer[10];char *pbuffer = buffer;while( (*pbuffer++ = getchar() )!= ...

  4. React + Redux 入门(一):抛开 React 学 Redux

    http://www.hacke2.cn/think-in-react-redux-1/

  5. ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)

    一.问题描述 1.ArcMap闪退 2.安装32位cx_oracle提示python目录不存在 二.解决方案 1.修改pythoncore的文件目录,指向C:\Python27\ArcGIS10.3\ ...

  6. Linux命令之乐--read

    read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量 -p:后接屏幕输出的提示语句 -n:设定输入的字符个数,当达到指定的个数则自动退出,并将输入赋予给变量 -t:当输 ...

  7. 在 Linux 中安装 Lighttpd Web 服务器

    Lighttpd 是一款开源 Web 服务器软件.Lighttpd 安全快速,符合行业标准,适配性强并且针对高配置环境进行了优化.相对于其它的 Web 服务器而言,Lighttpd 占用内存更少:因其 ...

  8. phpcms 列表页中调用其下的所有子栏目(或特定的子栏目)的方法

    phpcms 列表页中,如何调用其下的所有子栏目(或特定的子栏目),具体的写法如下,感兴趣的朋友可以参考下,希望对大家有所帮助 代码如下: {pc:get sql="select * fro ...

  9. c++ rand()

    一.C++中不能使用random()函数 random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过.但在C语言中int random(num)可以这样使用,它返回的是0至num-1的 ...

  10. webpack 报错 path is not defind

    webpack.config.js里的内容是这样的,注意标红的地方: 首先,绝对路径'./dist'是 没有问题的 那么,查了很多,最后看到别人的webpack.config.js里面这样写着,现在c ...