513.找树左下角的值
迭代法比较简单,层序遍历,找到最下面一层的第一个节点。题目已经说明节点数>=1了
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
queue = collections.deque()
queue.append(root)
result = root.val
while queue:
size = len(queue)
for i in range(size):
node = queue.popleft()
if i == 0:
result = node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return result
递归法
题解中遇到叶子节点并且当前深度比最大深度更大时更换结果值,但是最深的节点必定是叶子节点,所以不必判断是叶子节点
class Solution:
def __init__(self):
self.result = 0
self.max_depth = 0
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
self.getValue(root, 1)
return self.result
def getValue(self, node, depth):
if not node:
return
if depth > self.max_depth:
self.max_depth = depth
self.result = node.val
self.getValue(node.left, depth+1)
self.getValue(node.right, depth+1)
112. 路径总和
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
return self.traversal(root, 0, targetSum)
def traversal(self, node, cur_sum, target_sum):
if not node:
return False
cur_sum += node.val
if not node.left and not node.right:
if cur_sum == target_sum:
return True
return self.traversal(node.left, cur_sum, target_sum) or self.traversal(node.right, cur_sum, target_sum)
下面的代码思路更清晰
不要去累加然后判断是否等于目标和,那么代码比较麻烦,可以用递减,让计数器count初始为目标和,然后每次减去遍历路径节点上的数值。
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root:
return False
return self.traversal(root, targetSum-root.val)
def traversal(self, node, count):
if not node.left and not node.right:
return count == 0
if node.left:
if self.traversal(node.left, count-node.left.val):
return True
if node.right:
if self.traversal(node.right, count-node.right.val):
return True
return False
路径总和:返回是否存在路径
路径总和II:返回满足条件的所有路径
下面为路径总和II的代码
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
result = []
if not root:
return result
self.getPath(root, [root.val], result, targetSum-root.val)
return result
def getPath(self, node, path, result, count):
if not node.left and not node.right:
if count == 0:
result.append(path[:])
if node.left:
path.append(node.left.val)
self.getPath(node.left, path, result, count-node.left.val)
path.pop()
if node.right:
path.append(node.right.val)
self.getPath(node.right, path, result, count-node.right.val)
path.pop()
106.从中序与后序遍历序列构造二叉树
下面为每层递归定义了新的vector(就是数组),既耗时又耗空间。可以使用索引的方式,每次确定区间的左右索引
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if len(inorder) <= 0:
return
value = postorder[-1]
index = inorder.index(value)
left = self.buildTree(inorder[0:index], postorder[0:index])
right = self.buildTree(inorder[index+1:], postorder[index:-1])
return TreeNode(value, left, right)
105. 从前序与中序遍历序列构造二叉树
class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder:
return
value = preorder[0]
index = inorder.index(value)
left = self.buildTree(preorder[1:index+1], inorder[0:index])
right = self.buildTree(preorder[index+1:], inorder[index+1:])
return TreeNode(value, left, right)