Day18 | 513. 找树左下角的值 | 112.路径总和、113.路径总和ii | 105.106.从中序与后(前)序遍历序列构造二叉树
513. 找树左下角的值
本题递归偏难,反而迭代简单属于模板题, 两种方法掌握一下
题目链接/文章讲解/视频讲解:https://programmercarl.com/0513.找树左下角的值.html
思考
层序遍历秒了
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = deque()
queue.append(root)
res = []
while queue:
level = []
for i in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(level)
return res[-1][0]
112.路径总和、113.路径总和ii
本题 又一次涉及到回溯的过程,而且回溯的过程隐藏的还挺深,建议先看视频来理解
- 路径总和,和 113. 路径总和ii 一起做了。 优先掌握递归法。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0112.路径总和.html
112.路径总和
隐藏的回溯方法,sum每次提前减掉后传到下一步的。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
if root.left is None and root.right is None:
if root.val == targetSum:
return True
else:
return False
elif root.left and not root.right:
return self.hasPathSum(root.left,targetSum-root.val)
elif not root.left and root.right:
return self.hasPathSum(root.right,targetSum-root.val)
else:
return self.hasPathSum(root.right,targetSum-root.val) or self.hasPathSum(root.left,targetSum-root.val)
逻辑优化
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
if root.left is None and root.right is None:
if root.val == targetSum:
return True
else:
return False
if root.left:
if self.hasPathSum(root.left,targetSum-root.val):
return True
if root.right:
if self.hasPathSum(root.right,targetSum-root.val):
return True
return False
113.路径总和ii
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.sum = 0
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
path = []
res = []
def backtracking(root,targetSum):
if root is None:
return
path.append(root.val)
if root.left is None and root.right is None:
if self.sum + root.val == targetSum:
res.append(path[:])
return
if root.left:
self.sum+=root.val
backtracking(root.left,targetSum)
self.sum-=root.val
path.pop()
if root.right:
self.sum+=root.val
backtracking(root.right,targetSum)
self.sum-=root.val
path.pop()
backtracking(root,targetSum)
return res
105.106.从中序与后(前)序遍历序列构造二叉树
本题算是比较难的二叉树题目了,大家先看视频来理解。
106.从中序与后序遍历序列构造二叉树,105.从前序与中序遍历序列构造二叉树 一起做,思路一样的
题目链接/文章讲解/视频讲解:https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html
思考
前或者后序遍历,可以确定根节点位置,根据根节点位置去中序遍历中分割出中序的左右子树。根据左右子树的长度,再去前 或者后序找到对应遍历顺序的左 右子树。递归。
105.前、中序构建二叉树
##
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
# pre 中 左 右
# in 左 中 右
if len(preorder) == 0:
return None
root_val = preorder[0]
root = TreeNode(root_val)
index = inorder.index(root_val)
inorder_left = inorder[0:index]
inorder_right = inorder[index+1:]
preorder_left = preorder[1:len(inorder_left)+1]
preorder_right = preorder[len(inorder_left)+1:]
root.left = self.buildTree(preorder_left,inorder_left)
root.right = self.buildTree(preorder_right,inorder_right)
return root
106.后、中序构建二叉树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
# post 左 右 中
# in 左 中 右
if len(postorder) == 0:
return None
root_val = postorder[-1]
root = TreeNode(root_val)
index = inorder.index(root_val)
inorder_left = inorder[0:index]
inorder_right = inorder[index+1:]
postorder_left = postorder[0:len(inorder_left)]
postorder_right = postorder[len(inorder_left):-1]
root.left = self.buildTree(inorder_left,postorder_left)
root.right = self.buildTree(inorder_right,postorder_right)
return root
浙公网安备 33010602011771号