class Solution: def pathSum(self, root: TreeNode, total: int) -> List[List[int]]: results = [] stack = [] def dfs(root: TreeNode, total: int): if not root: return stack.append(root.val) total -= root.val if not root.left and not root.right and total == 0: results.append(stack.copy()) dfs(root.left, total) dfs(root.right, total) stack.pop() return dfs(root, total) return results sln = Solution() ans = sln.pathSum( TreeNode.from_heap([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]), 22) print(ans)
def mid_root_search(node): nonlocal counter_max, most_nums, current, counter if node is None: return mid_root_search(node.left) if node.val != current: if counter == counter_max: most_nums.append(current) elif counter > counter_max: most_nums = [current] counter_max = counter current = node.val counter = 1 else: counter += 1 mid_root_search(node.right) return mid_root_search(root) if counter == counter_max: most_nums.append(current) elif counter > counter_max: most_nums = [current] return most_nums root = TreeNode.from_heap([1, None, 2]) sln = Solution() ans = sln.findMode(root) print(ans)