1001 字
5 分钟
代码随想录算法训练营第十四天 | 二叉树 Part04

513. 树的左下角的值#

LeetCode 513. 树的左下角的值

class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.max_depth = float('-inf') # 初始化最大深度
self.res = 0 # 存储最终结果
self.traverse(root, 0) # 从深度 0 开始遍历
return self.res
def traverse(self, root, depth):
if not root.left and not root.right:
# 如果当前节点是叶子节点且深度大于最大深度,则更新结果
if depth > self.max_depth:
self.max_depth = depth
self.res = root.val
return
if root.left:
depth += 1
self.traverse(root.left, depth)
depth -= 1
if root.right:
depth += 1
self.traverse(root.right, depth)
depth -= 1

112. 路径总和#

LeetCode 112. 路径总和

class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
self.target = targetSum # 设置目标和
return self.traverse(root, root.val)
def traverse(self, root, sum_val):
# 如果当前节点是叶子节点且路径和等于目标值,则返回 True
if not root.left and not root.right and self.target == sum_val:
return True
# 如果当前节点是叶子节点但路径和不等于目标值,则返回 False
if not root.left and not root.right:
return False
if root.left:
sum_val += root.left.val
if self.traverse(root.left, sum_val):
return True
sum_val -= root.left.val # 回溯
if root.right:
sum_val += root.right.val
if self.traverse(root.right, sum_val):
return True
sum_val -= root.right.val # 回溯
return False

113. 路径总和 II#

LeetCode 113. 路径总和 II

class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.res = [] # 存储所有符合条件的路径
if not root:
return self.res
self.target = targetSum # 设置目标和
path = []
path.append(root.val) # 添加根节点值
self.traverse(root, root.val, path)
return self.res
def traverse(self, root, sum_val, path):
if not root.left and not root.right and self.target == sum_val:
# 找到一条路径,深拷贝加入结果
self.res.append(path[:])
return
if root.left:
sum_val += root.left.val
path.append(root.left.val)
self.traverse(root.left, sum_val, path)
sum_val -= root.left.val
path.pop() # 回溯
if root.right:
sum_val += root.right.val
path.append(root.right.val)
self.traverse(root.right, sum_val, path)
sum_val -= root.right.val
path.pop() # 回溯

105. 从前序与中序遍历序列构造二叉树#

LeetCode 105. 从前序与中序遍历序列构造二叉树

class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
# 用于记录中序遍历中每个值的索引,方便快速查找
self.index = {}
for i in range(len(inorder)):
self.index[inorder[i]] = i
return self.build(preorder, 0, len(preorder) - 1, inorder, 0, len(inorder) - 1)
def build(self, preorder, p_start, p_end, inorder, i_start, i_end):
# 当前序遍历的起始索引大于结束索引时,说明没有节点可以构造,返回 None
if p_start > p_end:
return None
# 前序遍历的第一个元素是根节点
root_val = preorder[p_start]
# 在中序遍历中找到根节点的索引
index = self.index[root_val]
# 计算左子树的大小
left_size = index - i_start
root = TreeNode(root_val)
# 递归构建左子树和右子树
# 左子树的前序遍历范围是从 p_start + 1 到 p_start + left_size
# 右子树的前序遍历范围是从 p_start + left_size + 1 到 p_end
# 左子树的中序遍历范围是从 i_start 到 index - 1
# 右子树的中序遍历范围是从 index + 1 到 i_end
root.left = self.build(preorder, p_start + 1, p_start + left_size, inorder, i_start, index - 1)
root.right = self.build(preorder, p_start + left_size + 1, p_end, inorder, index + 1, i_end)
return root

106. 从中序与后序遍历序列构造二叉树#

LeetCode 106. 从中序与后序遍历序列构造二叉树

class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
# 用于记录中序遍历中每个值的索引,方便快速查找
self.index = {}
for i in range(len(inorder)):
self.index[inorder[i]] = i
return self.build(inorder, 0, len(inorder) - 1, postorder, 0, len(postorder) - 1)
def build(self, inorder, i_start, i_end, postorder, p_start, p_end):
# 当前后序遍历的起始索引大于结束索引时,说明没有节点可以构造,返回 None
if i_start > i_end or p_start > p_end:
return None
root_val = postorder[p_end] # 后序遍历最后一个元素是根节点
index = self.index[root_val] # 在中序中找到根节点索引
left_size = index - i_start # 左子树节点数
root = TreeNode(root_val)
# 递归构建左子树和右子树
# 左子树的后序遍历范围是从 p_start 到 p_start + left_size - 1
# 右子树的后序遍历范围是从 p_start + left_size 到 p_end - 1
# 左子树的中序遍历范围是从 i_start 到 index - 1
# 右子树的中序遍历范围是从 index + 1 到 i_end
root.left = self.build(inorder, i_start, index - 1, postorder, p_start, p_start + left_size - 1)
root.right = self.build(inorder, index + 1, i_end, postorder, p_start + left_size, p_end - 1)
return root
代码随想录算法训练营第十四天 | 二叉树 Part04
https://fuwari.vercel.app/posts/code_musings_day_fourteen/
作者
Jarrett
发布于
2025-06-26
许可协议
CC BY-NC-SA 4.0