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:使用层次遍历解决,如果每一层都是对称的 那么该二叉树为对称(正好先做的层次遍历,发现这里可以直接用同样思路做,把空节点用' '空格代替 以保证对称)
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def isSymmetric(self, root: TreeNode) -> bool: 10 if root == None: 11 return True 12 layer = [root] 13 res = [] 14 while len(layer): 15 this_res = [] 16 next_l = [] 17 for n in layer: 18 if n == ' ': 19 this_res.append(' ') 20 continue 21 this_res.append(n.val) 22 if n.left: 23 next_l.append(n.left) 24 else: 25 next_l.append(' ') 26 if n.right: 27 next_l.append(n.right) 28 else: 29 next_l.append(' ') 30 for i in range(len(this_res)//2): 31 if this_res[i] != this_res[len(this_res)-i-1]: 32 return False 33 layer = next_l 34 35 return True
递归判断:
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 def judge(self, left, right): 10 if left == None and right == None: 11 return True 12 if left != None and right == None: 13 return False 14 if left == None and right != None: 15 return False 16 if left.val != right.val: 17 return False 18 return self.judge(left.right, right.left) and self.judge(left.left, right.right) 19 20 def isSymmetric(self, root: TreeNode) -> bool: 21 if root == None: 22 return True 23 return self.judge(root.left, root.right)
迭代:
def isSymmetric(self, root):if not root:return TruenodeList = [root.left,root.right]while nodeList:symmetricLeft = nodeList.pop(0)symmetricRight = nodeList.pop(0)if not symmetricLeft and not symmetricRight:continueif not symmetricLeft or not symmetricRight:return Falseif symmetricLeft.val != symmetricRight.val:return FalsenodeList.append(symmetricLeft.left)nodeList.append(symmetricRight.right)nodeList.append(symmetricLeft.right)nodeList.append(symmetricRight.left)return True
转载于:https://www.cnblogs.com/watch-fly/p/leetcode_101_watchfly.html
LeetCode 101 对称二叉树的几种思路(Python实现)相关推荐
- [leetcode] 101. 对称二叉树
101. 对称二叉树 一开始想复杂了呀,没有做出来 直观的思路就是反转这颗树,然后判断两颗树是不是相同的 class Solution {// 反转并创建一颗树public TreeNode reve ...
- leetcode 101. 对称二叉树 递归解法
题目 思路 一开始想要层序遍历二叉树,然后判断每一层是否回文,写着写着发现这样太麻烦了,看了评论区别人的递归解法,才写出来的. 二叉树的四种遍历方法: https://www.cnblogs.com/ ...
- LeetCode 101对称二叉树
力扣对称二叉树 思路: 1.递归 当根节点的左子树和右子树对称时,二叉树对称 根节点的左子树和右子树对称: 左子树的左子树和右子树的右子树相等 左子树的右子树和右子树的左子树相等 递归出口 (1)根节 ...
- Leetcode 101. 对称二叉树 解题思路及C++实现
方法一:递归 解题思路: 先判断当前根结点root是否为null,如果是,则返回true: 然后调用isSame函数,判断其左右节点是否满足对称要求: 在isSame函数中,核心思想在于递归比较 r1 ...
- LeetCode 101. 对称二叉树 思考分析
题目 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / 2 2 / \ / 3 4 4 3 但是下面这个 [1,2,2,null,3,null ...
- Leetcode 101.对称二叉树
Time: 20190901 题目描述 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1/ \2 2/ \ / \ 3 4 4 3 但是下面这个 ...
- leetcode - 101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3][1,2,2,3,4,4,3][1,2,2,3,4,4,3] 是对称的. 但是下面这个 [1,2,2,null,3 ...
- Leetcode 101.对称二叉树 (每日一题 20210709)
给定一个二叉树,检查它是否是镜像对称的.例如,二叉树 [1,2,2,3,4,4,3] 是对称的.1/ \2 2/ \ / \ 3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则 ...
- [leetcode]101.对称二叉树
给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1/ \2 2/ \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3 ...
最新文章
- 移动办公时代的工程行业怎么管理图像?大象云推出3D可视化整体解决方案
- Go 导入当前项目下的包
- tsconfig.json配置
- 美链BEC合约漏洞技术分析 1
- [蓝桥杯2018初赛]日志统计-双指针
- 【转载记录】Accessing Device Drivers from C#
- 详解-OTUS(大津法-最大类间方差)原理及C语言代码实现
- h3c s5500-28c-si操作
- 凸台可以延伸吗_知道对比度、清晰度和锐化的差别吗?操作不好,让一张照片报废...
- JSK-372 图案输出【入门】
- poj3159 Candies 2012-09-07
- 远程Redis服务器 JedisConnectionException: Failed connecting to host localhost:6379 解决方案
- C# 之 MouseHook(鼠标钩子)使用
- CNTV的点播调度方式
- 时空数据库中的轨迹数据压缩
- win系统分区表丢失后如何恢复
- c语言中语句开始的标志是,第一个单片机程序(C语言编写)
- 寻找美好,不如创造美好
- 常见的通讯协议总结(USART、IIC、SPI、485、CAN)
- gwu计算机硕士排名,美国乔治华盛顿大学计算机专业排名 研究方向有哪些