[leetcode tree]101. Symmetric Tree
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的更多相关文章
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- 【LeetCode】101 - Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- (Tree) 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【一天一道LeetCode】#101. Symmetric Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
随机推荐
- datagrid时间插件
jquery easyui日期控件中,在页面里用JS拿到设立的日期值的方法 链接:http://blog.csdn.net/liweibin_/article/details/13509917 jqu ...
- 【leetcode 简单】 第六十题 反转链表
反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可以迭代 ...
- Jenkins02:Jenkins+maven+svn集成
1.安装Maven并配置环境变量 下载maven(windows下载zip包,linux下载tar.gz包),然后配置环境变量 在项目中使用maven,可以从java中央仓库中获取到项目所依赖的jar ...
- 20165230 2017-2018-2 《Java程序设计》第5周学习总结
20165230 2017-2018-2 <Java程序设计>第5周学习总结 教材学习内容总结 第七章 内部类与异常类 内部类与外嵌类 可以在类中定义另一个类,即内部类 包含内部类的类为内 ...
- nginx_upstream_check_module-master对nginx的后端机器进行健康状态检查报403错误【转】
在nginx.conf配置文件中 在server添加 location /nstatus { check_status; access_log off; #allow 192.168.2.11; #d ...
- 畸形的 dockerfile中的COPY命令-
dockerfile中的COPY是指COPY 指定目录的“子级目录”下所有的目录和文件,到指定目录中,这个shell中的cp命令大相径庭,使得很多人纳闷,怎么cpy过去的文件不是自己想要的
- echarts 移动端地图数据可视化开发教程
如上效果图: 以下未代码: <!doctype html> <html lang="en"> <head> <meta charset ...
- java基础39 增强for循环(也叫foreach循环)
增强for循环是jdk1.5出现的新功能 1.增强for循环的作用 简化了迭代器的书写格式(注意:增强for循环底层还是使用了迭代器遍历) 2.增强for循环的格式 for(数据类型 变量名:遍历的目 ...
- JavaSE简单实现多线程聊天
1.1 主程序入口 在主程序入口处,通过设置MyWindow的第一个参数,如果为true则为服务器,如果为false,则为客户端,当然也可以设置第二个参数,区分客户端和服务器的窗口标题. public ...
- 树莓派3B安装远程
步骤1:树莓派3安装 RDP SERVER 及VNC SERVER sudo apt-get install -y tightvncserver sudo vncserver 最后才知道一定要加上VN ...