-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrange_sum_in_bst.cpp
72 lines (57 loc) · 2.09 KB
/
range_sum_in_bst.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* 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:
//recursive
int rangeSumBST(TreeNode* root, int low, int high) {
if (!root) return 0;
int ans = 0; //ans holds result of current node at hand.
if (low <= root->val && root->val <= high) { //current node within the boundaries [low, high]
ans += root->val;
}
//current node is not in boundaries [low, high]
//it means the range [low, high] can occupy children of current node, so we recurse
//disect the [low <= node.val <= high] condition into two parts...
//if current node is not in range and if current node is <= high - recurse for right half
if (root->val <= high) {
ans += rangeSumBST(root->right, low, high);
}
//if current node is not in range and if low <= current node - recurse for left half
if (low <= root->val) {
ans += rangeSumBST(root->left, low, high);
}
return ans;
}
//iterative
int rangeSumBST(TreeNode* root, int low, int high) {
if (!root) return 0;
int sum = 0;
stack<TreeNode *> st;
st.push(root);
while(!st.empty()) {
TreeNode *current = st.top();
st.pop();
if(!current)
continue;
if(low <= current->val and current->val <= high) {
sum += current->val;
}
if(low <= current->val) {
st.push(current->left);
}
if(current->val <= high) {
st.push(current->right);
}
}
return sum;
}
};