Insufficient Nodes in Root to Leaf Paths

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
    def sufficientSubset(self, root: Optional[TreeNode], limit: int) -> Optional[TreeNode]:
        if root.left == root.right:
            if root.val < limit:
                return None
            else:
                return root
        if root.left:
            root.left = self.sufficientSubset(root.left, limit - root.val)
        if root.right:
            root.right = self.sufficientSubset(root.right, limit - root.val)
        if root.left or root.right:
            return root
        else:
            return None