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.

这个题目思路就是BFS啦, 只不过把left child 和right child比较了之后再recursively 比较children的children. 我们用一个helper function, 用于比较tree1, 和tree2, 然后recursive call这个function, 然后返回helper(root, root)即可.

1. Constraints

1) root can be None, return True

2. Ideas

BFS,     T: O(n)   S: O(n)  worst case when tree is linear

3. Code

 class Solution:
def Symmetric(self, root):
def helper(tree1, tree2):
if not tree1 and not tree2:
return True
if tree1 and tree2 and tree1.val == tree2.val:
return helper(tree1.left, tree2.right) and helper(tree1.right, tree2.left)
return False
return helper(root, root)

4. Test cases

1) root is None

2) root is 1

3)

    1
/ \
2 2
/ \ / \
3 4 4 3

[LeetCode] 101. Symmetric Tree_ Easy tag: BFS的更多相关文章

  1. Leetcode 101. Symmetric Tree(easy)

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

  2. Leetcode之101. Symmetric Tree Easy

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

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

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

  4. [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

    We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...

  5. (二叉树 DFS 递归) 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 (对称树)

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

  7. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  8. Leetcode 101 Symmetric Tree 二叉树

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

  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. IMAP命令与分析

    https://www.cnblogs.com/crystalray/p/3304688.html Internet Mail Access Protocol(缩写为IMAP,以前称作交互邮件访问协议 ...

  2. centos 7安装jdk、tomcat

    jdk安装 创建上传目录: [root@ckl1 home]# pwd /home [root@ckl1 home]# mkdir upload 安装上传工具: yum install lrzsz 上 ...

  3. 剑指offer题目记录

    1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...

  4. CentOS 添加环境变量

      1.修改环境变量需要修改/etc/profile export PATH="$PATH:/usr/src/ruby-1.9.3-p0/ruby:/usr/local/bin/gem&qu ...

  5. How arduino IDE works?

    test.ino void setup() { pinMode(,OUTPUT); } void loop() { digitalWrite(,HIGH); delay(); digitalWrite ...

  6. kvm虚拟机不能使用virsh shutdownw命令关闭虚拟机的解决方法

    今天笔者在对kvm虚拟机进行管理时,使用virsh shutdown命令关闭指定的虚拟机时,发现虽然有如下的提示,但其实虚拟机却一直不会真正的关闭. 经过查看virsh命令帮助和上网查询,才得知vir ...

  7. [工具]Sublime 显示韩文

  8. php中属性和方法的修饰符

    <?php class A{ private function do1(){ echo "do1 called"; } protected function do2(){ e ...

  9. 涨知识,涨知识 :ThinkPHP框架下Where条件查询Mysql数据库某字段是否为空

    代码虐我千百遍,我对代码如初恋~ 问题: 查询某字段app_date数据是否为NULL,正常我们实现的办法是: $map['app_data'] = array('eq','null'); $data ...

  10. 静态类(static)与java值传递、引用传递小测

    java中都是值传递.直接上代码了: class TestStaticA { static { System.out.println("b"); } public TestStat ...