Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def all_paths_sum_k(root, k):
- """
- Args:
- root(BinaryTreeNode_int32)
- k(int32)
- Returns:
- list_list_int32
- """
- # Write your code here.
- if root is None:
- return []
- result = []
- def dfs(node, target, slate):
- ## base case - leaf node
- if node.right is None and node.left is None:
- if target == node.value:
- slate.append(node.value)
- result.append(slate[:])
- slate.pop()
- ## recursive case
- slate.append(node.value)
- if node.left is not None:
- dfs(node.left, target-node.value, slate)
- if node.right is not None:
- dfs(node.right, target-node.value, slate)
- slate.pop()
- dfs(root, k, [])
- paths = [[-1]] if len(result) == 0 else result
- return paths
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement