-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeSumOfBst.java
More file actions
142 lines (131 loc) · 3.97 KB
/
Copy pathRangeSumOfBst.java
File metadata and controls
142 lines (131 loc) · 3.97 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Source : https://leetcode-cn.com/problems/range-sum-of-bst/
// Author : cornprincess
// Date : 2021-04-27
/*****************************************************************************************************
*
* Given the root node of a binary search tree, return the sum of values of all nodes with a value in
* the range [low, high].
*
* Example 1:
*
* Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
* Output: 32
*
* Example 2:
*
* Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
* Output: 23
*
* Constraints:
*
* The number of nodes in the tree is in the range [1, 2 * 104].
* 1 <= Node.val <= 105
* 1 <= low <= high <= 105
* All Node.val are unique.
******************************************************************************************************/
package RangeSumOfBst;
import common.TreeNode;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
public class RangeSumOfBst {
// inorder-iterator
public int rangeSumBST(TreeNode root, int low, int high) {
int ans = 0;
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
TreeNode curr = stack.pop();
if (curr.val >= low && curr.val <= high) {
ans += curr.val;
}
root = curr.right;
}
return ans;
}
// inorder-iterator - purning
public int rangeSumBSTPruning(TreeNode root, int low, int high) {
int ans = 0;
Deque<TreeNode> stack = new LinkedList<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
// purning 根据BST的特性,左子树所有的值都会小于low
if (root.val < low) {
break;
}
root = root.left;
}
if (!stack.isEmpty()) {
TreeNode curr = stack.pop();
// core purning
if (curr.val > high) {
break;
}
if (curr.val >= low && curr.val <= high) {
ans += curr.val;
}
root = curr.right;
}
}
return ans;
}
int ans_recur = 0;
int low = 0;
int high = 0;
// inorder-recursive
public int rangeSumBST2(TreeNode root, int low, int high) {
this.low = low;
this.high = high;
recursive(root);
return ans_recur;
}
private void recursive(TreeNode root) {
if (root == null) {
return;
}
recursive(root.left);
if (root.val >= low && root.val <= high) {
ans_recur += root.val;
}
recursive(root.right);
}
// dfs
public int rangeSumBST3(TreeNode root, int low, int high) {
if (root == null) {
return 0;
}
if (root.val > high) {
return rangeSumBST3(root.left, low, high);
} else if (root.val < low) {
return rangeSumBST3(root.right, low, high);
} else {
return root.val + rangeSumBST3(root.left, low, high) + rangeSumBST3(root.right, low, high);
}
}
// bfs
public int rangeSumBST4(TreeNode root, int low, int high) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int ans = 0;
while (!queue.isEmpty()) {
TreeNode curr = queue.poll();
if (curr == null) {
continue;
}
if (curr.val > high) {
queue.add(curr.left);
} else if (curr.val < low) {
queue.add(curr.right);
} else {
ans += curr.val;
queue.add(curr.left);
queue.add(curr.right);
}
}
return ans;
}
}