题型分类
二叉树的性质相关题目
二叉树的遍历相关题目
路径和相关题目
二叉树的构建相关题目
二叉树的存储结构
class TreeNode(object):
def __init__(self,val):
self.val = val
self.left = None
self.right = None
二叉树的性质相关题目
100 相同的树
101 对称二叉树
110 平衡二叉树
104 二叉树的最大深度
111 二叉树的最小深度
662 二叉树最大宽度
100.相同的树
解法一:递归
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
# 四种终止条件
if p == None and q == None:
return True
if p == None or q == None:
return False
if p.val != q.val :
return False
# p q 的值相等
return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
解法二:迭代
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
stack = [(p,q)]
while stack:
a,b = stack.pop()
if a == None and b == None:
continue
if a and b and a.val == b.val :
stack.append((a.right,b.right))
stack.append((a.left,b.left))
else:
# 一个存在一个不存在、两个都存在但值不相等
return False
return True
101.对称二叉树
递归:
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
return self.sam(root.left,root.right)
def sam(self,left,right):
if left == None and right == None:
return True
if left == None or right == None:
return False
if left.val != right.val:
return False
return self.sam(left.left,right.right) and self.sam(left.right,right.left)
迭代:
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if root is None:
return True
stack = [(root.left,root.right)]
while stack:
a,b = stack.pop(0)
if a == None and b == None:
continue
if a == None or b == None:
return False
if a and b and a.val == b.val:
stack.append((a.left,b.right))
stack.append((a.right,b.left))
else:
return False
return True
110.平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点中的左右两个子树的高度差的绝对值不超过 1 。
解法一:自顶向下暴力递归,该方法中有大量的重复计算
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
if root == None:
return True
# 该结点的左子树和右子树的高度差小于1,即当前结点满足平衡树,则继续递归调用
if abs(self.depth(root.left)-self.depth(root.right))<= 1:
return self.isBalanced(root.left) and self.isBalanced(root.right)
else:
return False
# 求一个结点的深度
def depth(self,root):
if root is None:
return 0
if root.left == None and root.right == None:
return 1
if root.left != None or root.right != None:
return 1+max(self.depth(root.left), self.depth(root.right))
解法二:自底向上(提前阻断)
class Solution:
def isBalanced(self, root: TreeNode) -> bool:
if self.depth(root) == -1:
return False
else :
return True
def depth(self,root):
if root == None:
return 0
depth_left = self.depth(root.left)
if depth_left == -1:
return -1
depth_right = self.depth(root.right)
if depth_right == -1:
return -1
if abs(depth_left - depth_right)<=1:
# 因为到叶子结点的时候返回的是0,因此要加1
return 1 + max(depth_left,depth_right)
else :
return -1
104.二叉树的最大深度
方法一:迭代
迭代实现其实就是广度优先遍历,遍历每一个结点的高度,然后求得最深的一个结点的高度,那么就是整个树的高度了
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
stack= [(1,root)]
max_depth = 0
while stack:
cur_depth ,node = stack.pop(0)
if node:
# 只有当前的点不为空的时候才比较深度
max_depth = max(max_depth,cur_depth)
stack.append((cur_depth+1,node.left))
stack.append((cur_depth+1,node.right))
return max_depth
解法二:递归
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
return self.depth(root)
def depth(self,root):
if root == None:
return 0
return 1+max(self.depth(root.left),self.depth(root.right))
111.二叉树的最小深度
方法一:迭代
class Solution:
def minDepth(self, root: TreeNode) -> int:
if root == None:
return 0
stack = [(1,root)]
while stack:
depth,node = stack.pop(0)
# 只有一个结点的左结点和右结点都为None时该结点才为叶子结点
if node.left == None and node.right == None:
return depth
if node.left != None:
stack.append((depth+1,node.left))
if node.right != None:
stack.append((depth+1,node.right))
方法二:递归
class Solution:
def minDepth(self, root: TreeNode) -> int:
if root == None:
return 0
if root.left== None and root.right == None:
return 1
if root.left== None or root.right == None:
# 该结点的左右两个结点其中一个为空,但是该结点不是叶子结点,因此看左右子树的深的那一个
return 1+max(self.minDepth(root.left),self.minDepth(root.right))
return 1+min(self.minDepth(root.left),self.minDepth(root.right))
662.二叉树最大宽度
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
if root == None:
return 0
stack = [(root,1)]
width = 0
# stack为每一层中的所有点
while stack:
length = len(stack)
# 对这层中的每一个结点进行遍历
for i in range(length):
node,num = stack.pop(0)
# 该层中的第一个结点,None结点没有被加入
if i == 0 :
first_node = num
# 该层的最后一个结点
if i == length-1:
last_node = num
width = max(width,last_node - first_node+1)
# None结点不加入stack
if node.left != None:
stack.append((node.left,2*num))
if node.right != None:
stack.append((node.right,2*num+1))
return width