[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 ...
随机推荐
- SpringCloud微服务简介(一)
Spring Cloud简单认识 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及部署,并通过 ...
- 微服务深入浅出(3)-- 服务的注册和发现Eureka
现来说一些Eureka的概念: 1.服务注册 Register 就是Client向Server注册的时候提供自身元数据,比如IP和Port等信息. 2.服务续约 Renew Client默认每隔30s ...
- Oracle GoldenGate常用参数
OGG(Oracle GoldenGate)参数介绍 所有的GoldenGate进程均有参数文件 Manager Extract Replicat Utilities 所有参数均有缺省配置 实际应用只 ...
- 工具_HBuilder工具使用技巧
https://www.cnblogs.com/xiaohouzai/p/7696152.html
- Explorer : 发布一个key-value存储系统,带有客户端和服务器端
Explorer 一个key-value存储系统,带有客户端和服务器端.使用非常方便. 使用B+树作为存储引擎,客户端和服务器端使用TCP协议进行通信. 代码采用C++实现,底层将客户端和服务器通信封 ...
- angular 如何使用第三方组件ng-bootstrap
1.在你的项目中以下指令 npm install --save @ng-bootstrap/ng-bootstrap 安装完成会显示 + @ng-bootstrap/ng-bootstrap@1 ...
- Shell编程学习2--命令大全
Linux中有很多的命令,这些命令可分分为10类(具体参见[1]): 1) 文件管理; 2) 文档编辑; 3) 文件传输; 4) 磁盘管理; 5) 磁盘维护; 6) 网络通讯; 7) 系统管理; 8) ...
- TF-搞不懂的TF矩阵加法
看谷歌的demo mnist,卷积后加偏执量的代码 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)h_pool1 = max_pool ...
- 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver
广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...
- 转:vue-cli的webpack模板项目配置文件分析
转载地址:http://blog.csdn.net/hongchh/article/details/55113751 一.文件结构 本文主要分析开发(dev)和构建(build)两个过程涉及到的文件, ...