1080. Insufficient Nodes in Root to Leaf Paths ¶ Time: $O(n)$ Space: $O(h)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12class Solution { public: TreeNode* sufficientSubset(TreeNode* root, int limit) { if (root == nullptr) return nullptr; if (root->left == nullptr && root->right == nullptr) return root->val < limit ? nullptr : root; root->left = sufficientSubset(root->left, limit - root->val); root->right = sufficientSubset(root->right, limit - root->val); return root->left == nullptr && root->right == nullptr ? nullptr : root; } }; 1 2 3 4 5 6 7 8 9 10 11class Solution { public TreeNode sufficientSubset(TreeNode root, int limit) { if (root == null) return null; if (root.left == null && root.right == null) return root.val < limit ? null : root; root.left = sufficientSubset(root.left, limit - root.val); root.right = sufficientSubset(root.right, limit - root.val); return root.left == null && root.right == null ? null : root; } } 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution: def sufficientSubset( self, root: TreeNode | None, limit: int ) -> TreeNode | None: if not root: return None if not root.left and not root.right: return None if root.val < limit else root root.left = self.sufficientSubset(root.left, limit - root.val) root.right = self.sufficientSubset(root.right, limit - root.val) return None if not root.left and not root.right else root