给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
Python: 层序遍历
# 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:
if not root:
return None
ans = []
queue = collections.deque([root])
while queue:
path = []
for i in range(len(queue)):
cur = queue.popleft()
path.append(cur.val)
if cur.left: queue.append(cur.left)
if cur.right: queue.append(cur.right)
ans.append(path)
return ans[-1][0]
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
Python: 遍历
# 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
# Noting that the target should be decreased by root.val firstly
return self.preTravelsal(root, targetSum - root.val)
def preTravelsal(self, cur:TreeNode, target:int) -> bool:
# leaf node conditions
if cur.left == None and cur.right == None:
if target !=0:
return False
else:
return True
if cur.left:
target -= cur.left.val
if self.preTravelsal(cur.left, target):
return True
target += cur.left.val
if cur.right:
target -= cur.right.val
if self.preTravelsal(cur.right, target):
return True
target += cur.right.val
return False
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
# 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.path = []
self.ans = []
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.ans.clear()
self.path.clear()
if not root:
return []
self.path.append(root.val)
self.preTraversal(root, targetSum-root.val)
return self.ans
def preTraversal(self, cur:TreeNode, count:int):
# Stop condition
if cur.left == None and cur.right == None:
if count == 0:
self.ans.append(self.path[:])
return
else:
return
if cur.left:
self.path.append(cur.left.val)
count -= cur.left.val
self.preTraversal(cur.left, count)
count += cur.left.val
self.path.pop()
if cur.right:
self.path.append(cur.right.val)
count -= cur.right.val
self.preTraversal(cur.right, count)
count += cur.right.val
self.path.pop()
给定两个整数数组 preorder
和 inorder
,其中 preorder
是二叉树的先序遍历, inorder
是同一棵树的中序遍历,请构造二叉树并返回其根节点。
第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件.
第二步: 前序遍历的第一个就是当前的中间节点. (后序遍历则为最后一个)
第三步: 找中序遍历的切割点.
第四步: 切割inorder数组. 得到inorder数组的左,右半边.
第五步: 切割preorder数组. 得到preorder数组的左,右半边. (中序数组大小一定跟前序数组大小是相同的.)
第六步: 递归
第七步: 返回答案
# 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 or not preorder:
return None
root_val = preorder[0]
root = TreeNode(root_val)
cut_index = inorder.index(root_val)
inorder_left = inorder[:cut_index]
inorder_right = inorder[cut_index+1:]
# Noting the begin point and end point
preorder_left = preorder[1:1+len(inorder_left)]
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