class Solution:
def traversal(self, root):
if root is None:
return 0, root
if not root.left and not root.right:
return 1, root.val
depth_left, left_node_left = self.traversal(root.left)
depth_right, left_ndoe_right = self.traversal(root.right)
if depth_left >= depth_right:
return depth_left + 1, left_node_left
else: return depth_right + 1, left_ndoe_right
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
# 递归,返回结果?
# 返回深度?如果按先左后右的覆盖顺序,是不是第一个出现的就是最左
depth, left_node = self.traversal(root)
return left_node
# 层次遍历
q = collections.deque()
q.append(root)
while(q):
res = []
for i in range(len(q)):
node = q.popleft()
res.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return res[0]
题解里的递归:
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
self.max_depth = float('-inf')
self.result = None
self.traversal(root, 0)
return self.result
def traversal(self, node, depth):
if not node.left and not node.right:
if depth > self.max_depth:
self.max_depth = depth
self.result = node.val
return
if node.left:
depth += 1
self.traversal(node.left, depth)
depth -= 1
if node.right:
depth += 1
self.traversal(node.right, depth)
depth -= 1
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
return self.traversal(root, targetSum, 0)
def traversal(self, root, target, sum_):
if not root:
return False
sum_ += root.val # 这里加上了,后面是不是要回溯啊
if not root.left and not root.right: # 叶子节点就进行处理
if sum_ == target:
return True
else:
return False
left = self.traversal(root.left, target, sum_)
right = self.traversal(root.right, target, sum_)
sum_ -= root.val # 回溯?
return left or right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
return self.traversal(root, targetSum)
def traversal(self, root, target):
if not root:
return False
# sum_ += root.val # 这里加上了,后面是不是要回溯啊
if not root.left and not root.right: # 叶子节点就进行处理
if root.val == target:
return True
else:
return False
left = self.traversal(root.left, target-root.val)
right = self.traversal(root.right, target-root.val)
# sum_ -= root.val # 回溯?
return left or right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
path_res = []
self.path_sum(root, targetSum, [], path_res)
return path_res
def path_sum(self, root, target, path, path_res):
if not root:
return
path.append(root.val)
if not root.left and not root.right and root.val == target:
print(path)
path_res.append(list(path))
if root.left:
self.path_sum(root.left, target-root.val, path, path_res)
path.pop()
if root.right:
self.path_sum(root.right, target-root.val, path, path_res)
path.pop()
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
# 感觉可以递归
n = len(postorder)
if n == 0:
return
if n == 1:
return TreeNode(inorder[0])
mid = postorder[n-1]
# 找切割点.
i = inorder.index(mid)
mid_node = TreeNode(mid)
left = self.buildTree(inorder[0:i], postorder[0:i])
right = self.buildTree(inorder[i+1:], postorder[i:n-1])
mid_node.left = left
mid_node.right = right
return mid_node