513. 找树左下角的值
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
示例 1:
输入: root = [2,1,3] 输出: 1
示例 2:
输入: [1,2,3,4,null,5,6,null,null,7] 输出: 7
# 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
from collections import deque
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root:
return None
myque = deque([root]) #deque的方法就是把普通队列[]转为deque双向队列
result=[]
while myque:
level=[]
for i in range(len(myque)):
cur=myque.popleft()
level.append(cur)
if cur.left:
myque.append(cur.left)
if cur.right:
myque.append(cur.right)
result.append(level)
return result[-1][0].val
112. 路径总和
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 输出:true 解释:等于目标和的根节点到叶节点路径如上图所示。
示例 2:
输入:root = [1,2,3], targetSum = 5 输出:false 解释:树中存在两条根节点到叶子节点的路径: (1 --> 2): 和为 3 (1 --> 3): 和为 4 不存在 sum = 5 的根节点到叶子节点的路径。
示例 3:
输入:root = [], targetSum = 0 输出:false 解释:由于树是空的,所以不存在根节点到叶子节点的路径。
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
def hps(root,sum):
if not root: #终止条件
return False
sum-=root.val #中
if sum==0 and not root.left and not root.right: #判断是否等于target
return True
left= hps(root.left,sum) #左 sum不用回溯,因为sum值会还原
# sum+=root.left.val
right= hps(root.right,sum)#右
return left or right #要用到左右节点的值,不通过参数则通过return
# sum-=root.right.val
return hps(root,targetSum)
113. 路径总和 II
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22 输出:[[5,4,11,2],[5,8,4,5]]
示例 2:
输入:root = [1,2,3], targetSum = 5 输出:[]
示例 3:
输入:root = [1,2], targetSum = 0 输出:[]
# 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 pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
result=[]
def hps(root,sum,curline):
if not root: #终止条件
return
sum-=root.val #中
curline.append(root.val)
if sum==0 and not root.left and not root.right: #判断是否等于target
result.append(curline)
left= hps(root.left,sum,curline.copy())#左 curline不用回溯,因为copy是拷贝一份,不会改变原值
right= hps(root.right,sum,curline.copy())#右
#要用到左右节点的值,不通过参数则通过return
hps(root,targetSum,[])
return result
106. 从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
示例 1:
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 输出:[3,9,20,null,null,15,7]
示例 2:
输入:inorder = [-1], postorder = [-1] 输出:[-1]
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not inorder and not postorder:
return
#第一步:找到分割点
cut_num=postorder[-1]
cut_index=inorder.index(cut_num)
root=TreeNode(cut_num)
#第二步:分割中序
in_left=inorder[:cut_index]
in_right=inorder[cut_index+1:]
#第三步:分割后序
post_left=postorder[:cut_index]
post_right=postorder[cut_index:-1]
#第四步:递归
root.left=self.buildTree(in_left,post_left)#左
root.right=self.buildTree(in_right,post_right)#右
return root
105. 从前序与中序遍历序列构造二叉树
给定两个整数数组 preorder
和 inorder
,其中 preorder
是二叉树的先序遍历, inorder
是同一棵树的中序遍历,请构造二叉树并返回其根节点。
示例 1:
输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] 输出: [3,9,20,null,null,15,7]
示例 2:
输入: preorder = [-1], inorder = [-1] 输出: [-1]
# 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]:
if not inorder and not preorder:
return
#第一步:找到分割点
cut_num=preorder[0]
cut_index=inorder.index(cut_num)
root=TreeNode(cut_num)
#第二步:分割中序
in_left=inorder[:cut_index]
in_right=inorder[cut_index+1:]
#第三步:分割前序
pre_left=preorder[1:cut_index+1]
pre_right=preorder[cut_index+1:]
#第四步:递归
root.left=self.buildTree(pre_left,in_left)#左
root.right=self.buildTree(pre_right,in_right)#右
return root