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

判断一棵树是否对称

递归:

 class Solution(object):
def isSymmetric(self, root):
if not root:
return True
return self.issym(root.left,root.right)
def issym(self,left,right):
if not left and not right:
return True
if not left or not right:
return False
if left.val == right.val:
return self.issym(left.left,right.right) and self.issym(left.right,right.left)
return False

非递归:

递归本质上就是栈,所以。。。

 class Solution(object):
def isSymmetric(self, root):
if not root:
return True
stack=[[root.left,root.right]]
while stack:
left,right = stack.pop()
if not left and not right:
continue
if not left or not right:
return False
if left.val == right.val:
stack.append([left.right,right.left])
stack.append([left.left,right.right])
else:
return False
return True

[leetcode tree]101. Symmetric Tree的更多相关文章

  1. Leetcode之101. Symmetric Tree Easy

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

  2. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  3. 【LeetCode】101 - Symmetric Tree

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

  4. LeetCode OJ 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 (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  6. (Tree) 101. Symmetric Tree

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

  7. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

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

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

随机推荐

  1. 20155230 2016-2017-2《Java程序设计》第六周学习总结

    20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度 ...

  2. 【leetcode 简单】 第九十六题 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  3. 【codeforces】【比赛题解】#855 Codefest 17

    神秘比赛,以<哈利波特>为主题……有点难. C题我熬夜切终于是写出来了,可惜比赛结束了,气啊. 比赛链接:点我. [A]汤姆·里德尔的日记 题意: 哈利波特正在摧毁神秘人的分灵体(魂器). ...

  4. Python标准库笔记(11) — Operator模块

    Operator--标准功能性操作符接口. 代码中使用迭代器时,有时必须要为一个简单表达式创建函数.有些情况这些函数可以用一个lambda函数实现,但是对于某些操作,根本没必要去写一个新的函数.因此o ...

  5. vue+elementui 新增和编辑如何实现共用一个弹框

    //html代码: //按钮 <el-button type="primary" size="medium" @click="addEquipm ...

  6. Ubuntu下使用virtualenv

    Ubuntu 18.04,Python 3.6.5(最新3.7),virtualenv 16.0.0, 即将在Ubuntu上大张旗鼓地干活啦!那么,将之前安装的virtualenv运行起来吧(前面都是 ...

  7. RStdio常用快捷键

    掌握常用快捷键的使用, 可以让我们的效率加倍! 下面只列举了一些最常见的快捷键, RStdio的快捷键远不止这些, 具体的可以参照这里. 无论是在控制台还是在编辑区, Tab都可以帮助我们对代码进行补 ...

  8. linux定时任务-cron

    /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 /sbin/service crond restart //重启服务 ...

  9. 面试经典问题---数据库索引B+、B-树

    具体讲解之前,有一点,再次强调下:B-树,即为B树.因为B树的原英文名称为B-tree,而国内很多人喜欢把B-tree译作B-树,其实,这是个非常不好的直译,很容易让人产生误解.如人们可能会以为B-树 ...

  10. Linux基础入门学习笔记之三

    第四节 Linux目录结构及文件基本操作 Linux目录结构 Linux 的目录与 Windows 的目录的区别 目录与存储介质(磁盘,内存,DVD 等)的关系 Windows 一直是==以存储介质为 ...