513.找树左下角的值
题目链接/文章讲解/视频讲解:https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html
- 考点
- 层序遍历
- 我的思路
- 题目中要求找到二叉树中最底层最左边的节点,因此使用层序遍历,并将每层第一个节点赋值给结果变量,则遍历结束时结果变量就储存了树左下角的值
- 视频讲解关键点总结
- 未看讲解
- 我的思路的问题
- 无
- 代码书写问题
- 无
- 可执行代码
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
layer = [root]
result = root.val
while layer:
count = len(layer)
for i in range(count):
cur = layer.pop(0)
if i == 0:
result = cur.val
if cur.left is not None:
layer.append(cur.left)
if cur.right is not None:
layer.append(cur.right)
return result
112. 路径总和
题目链接/文章讲解/视频讲解:https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html
- 考点
- 递归+回溯
- 我的思路
- 递归三要素
- 形参:当前节点,目标值,当前总和;返回值为True或null
- 退出条件:如果当前节点为叶子节点,判断当前总和与目标值是否相等,相等return True,否则return False
- 递归逻辑:
- 在终止条件之前先把当前节点的值加到总和上,否则做退出条件判断时会遗漏当前节点的值
- 终止条件后
- 如果左子节点不为空,则左递归,并判断其返回值是否为True,如果为True,直接返回True(由于和值没有作为返回值传递回来,因此当前和并未改变,因此不需要额外的回溯操作)
- 如果右子节点不为空,则右递归,并判断其返回值是否为True,如果为True,直接返回True
- 递归三要素
- 视频讲解关键点总结
- 和我的思路类似
- 我的思路的问题
- 无
- 代码书写问题
- 无
- 可执行代码
class Solution:
def cal_tar(self, root, targetSum, summation):
summation += root.val
if root.left is None and root.right is None:
if targetSum == summation:
return True
else:
return False
if root.left is not None:
flag = self.cal_tar(root.left, targetSum, summation)
if flag == True:
return True
if root.right is not None:
flag = self.cal_tar(root.right, targetSum, summation)
if flag == True:
return True
return False
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
return self.cal_tar(root, targetSum, 0)
*106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树
题目链接/文章讲解/视频讲解:https://programmercarl.com/0106.%E4%BB%8E%E4%B8%AD%E5%BA%8F%E4%B8%8E%E5%90%8E%E5%BA%8F%E9%81%8D%E5%8E%86%E5%BA%8F%E5%88%97%E6%9E%84%E9%80%A0%E4%BA%8C%E5%8F%89%E6%A0%91.html
- 考点
- 递归
- 我的思路
- 无
- 视频讲解关键点总结
- 通过前/中/后序的结果恢复二叉树需要两个分割点来分割左、右、中的区间位置,因此必须提供前/中或后/中的组合,单独的前后无法恢复二叉树的结构
- 递归三要素
- 形参:中序列表、前/后序列表;返回值为当前节点
- 退出条件:判断前/后序结果列表是否为空,如果为空,说明当前二叉(子)树为空,此时返回None
- 递归逻辑:
- 将前/后序结果的第一个/最后一个元素切割出来,即为当前二叉(子)树的根节点
- 利用根节点,在中序的结果中分割出左子树和右子树
- 再利用中序的左子树和右子树的大小,在前/后序的结果里分割出左右子树
- 利用分割得到的中序左子树和前/后序左子树进行左递归
- 利用分割得到的中序右子树和前/后序右子树进行右递归
- 返回当前节点
- 我的思路的问题
- 无思路
- 代码书写问题
- 判断列表是否为空可以直接把列表对象作为判断条件,其为空时会自动转换为False
- 可执行代码
# 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(val=root_val)
left_position, right_position = 0, 0
for index in range(len(inorder)):
if inorder[index] == root.val:
left_position = index
break
right_position = left_position + 1
root.left = self.buildTree(inorder[:left_position], postorder[:left_position])
root.right = self.buildTree(inorder[right_position:], postorder[left_position:-1])
return root
# 105.从前序与中序遍历序列构造二叉树
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder:
return None
root = TreeNode(val=preorder[0])
pos = 0
for i in range(len(inorder)):
if inorder[i] == preorder[0]:
pos = i
break
root.left = self.buildTree(preorder[1:pos + 1], inorder[:pos])
root.right = self.buildTree(preorder[pos + 1:], inorder[pos + 1:])
return root