代码随想录算法训练营第十八天 | leetcode 513.找树左下角的值,112.路径总和,113.路径总和ii,106.从中序与后序遍历序列构造二叉树,105.从前序与中序遍历序列构造二叉树
513.找树左下角的值
题目:给定一个二叉树,在树的最后一行找到最左边的值。假设二叉树中至少有一个节点。
题目链接:513.找树左下角的值
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
max_depth = -float("INF")
leftmost_val = 0
def _traverse(root, cur_depth):
# 内层函数未定义这两个变量, 通过关键字nonlocal 直接使用这两个变量
nonlocal max_depth, leftmost_val
if not root.left and not root.right:
if cur_depth > max_depth:
max_depth = cur_depth
leftmost_val = root.val
if root.left:
cur_depth += 1
_traverse(root.left, cur_depth)
cur_depth -= 1 # 回溯
if root.right:
cur_depth += 1
_traverse(root.right, cur_depth)
cur_depth -= 1 # 回溯
_traverse(root, 0)
return leftmost_val
112.路径总和
题目:给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
题目链接:112. 路径总和
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def isornot(root, targetSum):
if not root.left and not root.right and targetSum ==0 :
return True
if not root.left and not root.right:
return False
if root.left:
targetSum -= root.left.val
if isornot(root.left, targetSum):
return True
targetSum += root.left.val
if root.right:
targetSum -= root.right.val
if isornot(root.right, targetSum):
return True
targetSum += root.right.val
return False
if root == None: #树为空的情况
return False
else:
return isornot(root, targetSum - root.val)
113.路径总和ii
题目:给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。
题目链接:113. 路径总和 II
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
result, path = [], []
if not root:
return []
def travelsal(node, remain):
if not node.left and not node.right:
if remain == 0:
result.append(path[:])
return
if node.left:
path.append(node.left.val)
travelsal(node.left, remain - node.left.val)
path.pop()
if node.right:
path.append(node.right.val)
travelsal(node.right, remain - node.right.val)
path.pop()
path.append(root.val)
travelsal(root, targetSum - root.val)
return result
106.从中序与后序遍历序列构造二叉树
题目:根据一棵树的中序遍历与后序遍历构造二叉树。
注意: 你可以假设树中没有重复的元素。
题目链接:106. 从中序与后序遍历序列构造二叉树
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not postorder:
return None
root_val = postorder[-1]
root = TreeNode(root_val)
seperate_idx = inorder.index(root_val)
inorder_left = inorder[:seperate_idx]
inorder_right = inorder[seperate_idx + 1:]
postorder_left = postorder[:len(inorder_left)]
postorder_right = postorder[len(inorder_left): len(postorder) - 1]
root.left = self.buildTree(inorder_left, postorder_left)
root.right = self.buildTree(inorder_right, postorder_right)
return root
105.从前序与中序遍历序列构造二叉树
题目:根据一棵树的前序遍历与中序遍历构造二叉树。
注意: 你可以假设树中没有重复的元素。
题目链接:105. 从前序与中序遍历序列构造二叉树
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder:
return None
root_val = preorder[0]
root = TreeNode(root_val)
seperate_index = inorder.index(root_val)
inorder_left = inorder[:seperate_index]
inorder_right = inorder[seperate_index + 1:]
preorder_left = preorder[1:1 + len(inorder_left)]
preorder_right = preorder[1 + len(inorder_left):]
root.left = self.buildTree(preorder_left, inorder_left)
root.right = self.buildTree(preorder_right, inorder_right)
return root