LeetCode 101 对称二叉树的几种思路(Python实现)
对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3]
是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3 但是下面这个[1,2,2,null,3,null,3]
则不是镜像对称的:
1
/ \
2 2
\ \
3 3 思路1:使用层次遍历解决,如果每一层都是对称的 那么该二叉树为对称(正好先做的层次遍历,发现这里可以直接用同样思路做,把空节点用' '空格代替 以保证对称)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
layer = [root]
res = []
while len(layer):
this_res = []
next_l = []
for n in layer:
if n == ' ':
this_res.append(' ')
continue
this_res.append(n.val)
if n.left:
next_l.append(n.left)
else:
next_l.append(' ')
if n.right:
next_l.append(n.right)
else:
next_l.append(' ')
for i in range(len(this_res)//2):
if this_res[i] != this_res[len(this_res)-i-1]:
return False
layer = next_l return True
递归判断:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def judge(self, left, right):
if left == None and right == None:
return True
if left != None and right == None:
return False
if left == None and right != None:
return False
if left.val != right.val:
return False
return self.judge(left.right, right.left) and self.judge(left.left, right.right) def isSymmetric(self, root: TreeNode) -> bool:
if root == None:
return True
return self.judge(root.left, root.right)
迭代:
def isSymmetric(self, root):
if not root:
return True
nodeList = [root.left,root.right]
while nodeList:
symmetricLeft = nodeList.pop(0)
symmetricRight = nodeList.pop(0)
if not symmetricLeft and not symmetricRight:
continue
if not symmetricLeft or not symmetricRight:
return False
if symmetricLeft.val != symmetricRight.val:
return False
nodeList.append(symmetricLeft.left)
nodeList.append(symmetricRight.right)
nodeList.append(symmetricLeft.right)
nodeList.append(symmetricRight.left)
return True
LeetCode 101 对称二叉树的几种思路(Python实现)的更多相关文章
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- LeetCode 101. 对称二叉树(Symmetric Tree)
题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null, ...
- LeetCode 101.对称二叉树 - JavaScript
题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...
- php实现求对称二叉树(先写思路,谋而后动)
php实现求对称二叉树(先写思路,谋而后动) 一.总结 1.先写思路,谋而后动 二.php实现求对称二叉树 题目描述: 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的 ...
- LeetCode【101. 对称二叉树】
对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...
- Leetcode题目101.对称二叉树(简单)
题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...
- 【LeetCode】101. 对称二叉树
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3, ...
- 领扣(LeetCode)对称二叉树 个人题解
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...
- 【Leetcode】对称二叉树
递归法 执行用时 :12 ms, 在所有 C++ 提交中击败了43.44%的用户 内存消耗 :14.6 MB, 在所有 C++ 提交中击败了95.56%的用户 /** * Definition for ...
随机推荐
- x64位windows 2003 server中“Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败”问题
给朋友看一个老asp网站图片不能上传问题,试过网上各种办法都提示: Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败 最终问题出在x6 ...
- testng参数化(提供测试数据)
testng提供测试数据的两个注释:@DataProvide和@Parameter 一.通过testng.xml中设置参数 (实际上testng.xml只是一个名字,可以起任何一个名字,只要是.x ...
- 63. Unique Paths II (Graph; DP)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- leetcode 121 股票买卖问题系列
描述: 给一些列数字,表示每条股票的价格,如果可以买卖一次(不能同一天买和卖),求最大利益(即差最大). 其他三道问题是,如果能买卖无限次,买卖两次,买卖k次. 题一: 实质是求后面一个数减前一个数的 ...
- 为什么数组没有实现Iterable接口,但可以使用foreach语句遍历
在Java中,对于数组为什么能够使用foreach语句一直感觉很困惑. 对于能够使用foreach语句进行遍历的对象,只有两种情况,其中一种是遍历对象必须实现Iterable接口,实现ierator( ...
- Cookie、Session、Token
一.发展史 .最初.Web基本上就是文档的浏览而已,既然是浏览,作为服务器,不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议,就是请求加相应,尤其是我不用记住是谁刚刚发了H ...
- Associate File Type with Qt In Mac Os and Win
Win Registry Question One day, my boss want me to finish one function which let the users can double ...
- Python操作mysql之模块pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文环境 python3.6.1 Mysql ...
- Work at a KFC fast food restaurant
During the summer holiday of 2005,I thought I should do some meaningful instead of at home and watch ...
- canvas里调用getImageData的报security的问题
canvas元素支持绘制任意图片元素: var ctx = document.getElementById("canvas").getContext("2d") ...