-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1080. Insufficient Nodes in Root to Leaf Paths.cpp
42 lines (42 loc) · 1.38 KB
/
1080. Insufficient Nodes in Root to Leaf Paths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* sufficientSubset(TreeNode* root, int limit) {
if (!root) return nullptr;
if (dfs(root, limit, 0) < limit) return nullptr;
return root;
}
int dfs(TreeNode* root, int limit, int presum) { // Return max path sum of node
if (root->left && root->right) {
int l = dfs(root->left, limit, presum + root->val);
int r = dfs(root->right, limit, presum + root->val);
presum = max(l,r);
if (l < limit) root->left = nullptr;
if (r < limit) root->right = nullptr;
}
else if (root->right) {
int r = dfs(root->right, limit, presum + root->val);
if (r < limit) root->right = nullptr;
presum = r;
}
else if (root->left) {
int l = dfs(root->left, limit, presum + root->val);
if (l < limit) root->left = nullptr;
presum = l;
}
else {
presum = presum + root->val;
}
return presum;
}
};