Skip to content

Commit 7f3316d

Browse files
authored
Create insufficient-nodes-in-root-to-leaf-paths.cpp
1 parent d333c8c commit 7f3316d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Time: O(n)
2+
// Space: O(h)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* struct TreeNode {
7+
* int val;
8+
* TreeNode *left;
9+
* TreeNode *right;
10+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11+
* };
12+
*/
13+
class Solution {
14+
public:
15+
TreeNode* sufficientSubset(TreeNode* root, int limit) {
16+
if (!root) {
17+
return nullptr;
18+
}
19+
if (!root->left && !root->right) {
20+
return root->val < limit ? nullptr : root;
21+
}
22+
root->left = sufficientSubset(root->left, limit - root->val);
23+
root->right = sufficientSubset(root->right, limit - root->val);
24+
if (!root->left && !root->right) {
25+
return nullptr;
26+
}
27+
return root;
28+
}
29+
};

0 commit comments

Comments
 (0)