-
Notifications
You must be signed in to change notification settings - Fork 21
/
Search Range in Binary Search Tree .java
executable file
·76 lines (60 loc) · 1.87 KB
/
Search Range in Binary Search Tree .java
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
M
1528177257
tags: BST, Binary Tree
给一个BST, integer range (k1, k2), 找range 里面所有的integer.
#### BST
- 等于dfs遍历了所有k1<= x <= k2的x node。
- dfs left, process root, then dfs right
- 这里, 把 left/right/match的情况全部cover了,然后把k1,k2的边框限制好,中间就全部遍历了。
```
/*
Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree.
Find all the keys of tree in range k1 to k2.
i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order.
Example
For example, if k1 = 10 and k2 = 22, then your function should print 12, 20 and 22.
20
/ \
8 22
/ \
4 12
Tags Expand
Binary Tree Binary Search Tree
Recursive
Thinking Process:
Find lowest and turn around.
If don’t hit the ground, keep digging:
if (root.val > k1) dig into root.left
Check current
Find maximum and turn around.
If don’t hit the ceiling, keep climbing:
if (root.val < k2) climb to root.right
*/
public class Solution {
/**
* @param root: param root: The root of the binary search tree
* @param k1: An integer
* @param k2: An integer
* @return: return: Return all keys that k1<=key<=k2 in ascending order
*/
public List<Integer> searchRange(TreeNode root, int k1, int k2) {
List<Integer> result = new ArrayList<>();
helper(result, root, k1, k2);
return result;
}
public void helper(List<Integer> result, TreeNode root, int k1, int k2) {
if (root == null) {
return;
}
if (root.val > k1) {
helper(result, root.left, k1, k2);
}
if (root.val >= k1 && root.val <= k2) {
result.add(root.val);
}
if (root.val < k2) {
helper(result, root.right, k1, k2);
}
}
}
```