513.找树左下角的值
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
解题思路:
层序遍历,return最后一行的第一个值。
代码如下:
# 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:
deque = collections.deque()#store the node
deque.append(root)#store the first node, root
while deque:
lst =[]#store node each layer
for i in range(len(deque)):
cur = deque.popleft()
lst.append(cur)#store every node of each layer
if i == 0:
#store the first node
result = lst[-1]
if cur.left:
deque.append(cur.left)
if cur.right:
deque.append(cur.right)
return result
112. 路径总和
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
解题思路:
设置count=target_number进行递归遍历,对每个结点进行减法运算
递归参数输入:def dfs(root, count)
停止条件:当count=0并到达叶子节点,return true,否则return false
递归逻辑:左右递归
# 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 not root:
return False
count = targetSum-root.val
def dfs(root, count):
if not root.left and not root.right and count == 0:
return True
if not root.left and not root.right and count != 0:
return False
if root.left:
count -= root.left.val
if dfs(root.left, count):
return True
count+= root.left.val
if root.right:
count -= root.right.val
if dfs(root.right, count):
return True
count += root.right.val
return False
return dfs(root, count)
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 pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
result = []
path = []
count = targetSum
def dfs(root, count, path, result):
if not root:
return
path.append(root.val)
count -= root.val
# print(path, list(path))
if not root.left and not root.right and count == 0:
result.append(list(path))
dfs(root.left, count, path, result)
dfs(root.right, count, path, result)
path.pop()
dfs(root, count, path, result)
return result
106.从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
解题思路:
注意返回类型为二叉树
根据中序遍历和后序遍历的特性,后序遍历的最后一个node一定是中介的根节点,因此在中序遍历中定位该node的index,以此区分左右子树得到left_in, right_in。注意中序遍历中左右子树的长度与后序遍历中左右子树的长度相同,因此可以在后序遍历中区分左右子树得到left_post, right_post,将left_in, left_post与right_in, right_post放入递归,返回root。
# 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]:
# root = TreeNode()
def binary(inorder, postorder):
if not postorder:
return None
root_node = postorder.pop()
index = inorder.index(root_node)
root = TreeNode(root_node)
left_in = inorder[:index]#左中序
right_in = inorder[index+1:]#右中序
size = len(left_in)
left_post = postorder[:size]#左后序
right_post = postorder[size:]#右后序
root.left = binary(left_in, left_post)
root.right = binary(right_in, right_post)
return root
return binary(inorder, postorder)