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. 六、K3 WISE 开发插件《直接SQL报表开发新手指导 - BOM成本报表》

    ======================== 目录: 1.直接SQL报表 ======================== 1.直接SQL报表 以BOM成本报表为例,在销售模块部署,需要购买[金蝶 ...

  2. redhat vi 命令

    转载:http://www.cnblogs.com/zhanglong0426/archive/2010/10/07/1845268.html http://blog.sina.com.cn/s/bl ...

  3. Artech的MVC4框架学习——第七章Action的执行

    概况:Action的执行不仅包含action方法的执行,还包含相关筛选器的执行. 第一基于线程池的请求,http请求称谓工作线程(p321),基于线程池优势:工作线程重用和工作线程数量的限制. 第二两 ...

  4. C语言递归练习

    1.炮弹一样的球状物体,能够堆积成一个金字塔,在顶端有一个炮弹,它坐落在一个4个炮弹组成的层面上,而这4个炮弹又坐落在一个9个炮弹组成的层面上,以此类推.写一个递归函数CannonBall,这个函数把 ...

  5. 【咸鱼教程】本地图片上传。动态GIF表情图生成

    本案例参考:http://emoji.decathlon.trustingme.cn/但是实现方式不一样. 教程目录一 head first二 打开本地图片功能三 拖拽和缩放手势,调整图片四 gifj ...

  6. java.lang.instrument 中的premain 实现类的个性化加载(附源代码)

    背景 想调用ASM API (用于字节码处理的开源API)对字节码进行处理,目标是实现对java程序运行时各种对象的动态跟踪,并进一步分析各个对象之间的关系(研究前提是目前的UML锁阐释的whole- ...

  7. mac常用工具

    这里我整理一下,mac上经常要用的的工具(仅供参考): Homebrew HomeBrew是Mac下面的一个包管理器,方便我们安装一些Mac OS没有的UNIX工具.软件. iTerm2 iTerm2 ...

  8. mysql if判断

    select if(SUBSTR('06622200556',1,2)='06',0,1) from t_member_product_adb limit 2 输出结果为:0,0

  9. HBase一次客户端读写异常解读分析与优化全过程(干货)

    大数据时代,HBase作为一款扩展性极佳的分布式存储系统,越来越多地受到各种业务的青睐,以求在大数据存储的前提下实现高效的随机读写操作.对于业务方来讲,一方面关注HBase本身服务的读写性能,另一方面 ...

  10. logstash实战filter插件之grok(收集apache日志)

    有些日志(比如apache)不像nginx那样支持json可以使用grok插件 grok利用正则表达式就行匹配拆分 预定义的位置在 /opt/logstash/vendor/bundle/jruby/ ...