diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1080. Insufficient Nodes in Root to Leaf Paths.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1080. Insufficient Nodes in Root to Leaf Paths.py" new file mode 100644 index 00000000..76290976 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1080. Insufficient Nodes in Root to Leaf Paths.py" @@ -0,0 +1,28 @@ +# 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 sufficientSubset(self, root: TreeNode | None, limit: int) -> TreeNode | None: + + def dfs(node, prev_total): + if not node: + return None + + node_total = prev_total + node.val + + if not node.left and not node.right: # leaf + return node if node_total >= limit else None + + node.left = dfs(node.left, node_total) + node.right = dfs(node.right, node_total) + + if not node.left and not node.right: # become leaf + return None + + return node + + return dfs(root, 0) +