Skip to content

Commit e5b9fb7

Browse files
committed
03.02 (1) search range on BST using pruning
1 parent 46b313b commit e5b9fb7

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : Binary Search Tree
4+
* @by : Steven Cooks
5+
* @date: Sep 1, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
* Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary
10+
* Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all
11+
* x such that k1<=x<=k2 and x is a key of given BST. Return all the keys
12+
* in ascending order.
13+
*
14+
***************************************************************************
15+
* {@link http://www.lintcode.com/en/problem/search-range-in-binary-search-tree/ }
16+
*/
17+
package _02_SearchRangeInBinarySearchTree;
18+
19+
import java.util.ArrayList;
20+
21+
import com.leetcode.TreeNode;
22+
23+
/** see test {@link _02_SearchRangeInBinarySearchTree.SolutionTest } */
24+
public class Solution {
25+
26+
/**
27+
* @param root: The root of the binary search tree.
28+
* @param k1 and k2: range k1 to k2.
29+
* @return: Return all keys that k1<=key<=k2 in ascending order.
30+
*/
31+
public ArrayList<Integer> searchRange(TreeNode root, int k1, int k2) {
32+
ArrayList<Integer> res = new ArrayList<>();
33+
if (root == null) {
34+
return res;
35+
}
36+
int val = root.val;
37+
if (val >= k1) {
38+
// pruning
39+
res.addAll(searchRange(root.left, k1, k2));
40+
}
41+
if (val >= k1 && val <= k2) {
42+
res.add(val);
43+
}
44+
if (val <= k2) {
45+
// pruning
46+
res.addAll(searchRange(root.right, k1, k2));
47+
}
48+
return res;
49+
}
50+
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package _02_SearchRangeInBinarySearchTree;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Rule;
10+
import org.junit.Test;
11+
import org.junit.rules.Timeout;
12+
13+
import com.leetcode.TreeNode;
14+
15+
public class SolutionTest {
16+
17+
/** Test method for {@link _02_SearchRangeInBinarySearchTree.Solution } */
18+
Solution solution;
19+
20+
@Rule
21+
public Timeout globalTimeout = new Timeout(200);
22+
23+
@Before
24+
public void setUp() throws Exception {
25+
solution = new Solution();
26+
}
27+
28+
@After
29+
public void tearDown() throws Exception {
30+
solution = null;
31+
}
32+
33+
// 1
34+
@Test
35+
public void Test1() {
36+
TreeNode root = TreeNode.getBST1();
37+
int k1 = 0;
38+
int k2 = 2;
39+
ArrayList<Integer> actual = solution.searchRange(root, k1, k2);
40+
ArrayList<Integer> expected = new ArrayList<>();
41+
expected.add(1);
42+
assertEquals(expected, actual);
43+
}
44+
45+
// 1
46+
// \
47+
// 2
48+
// \
49+
// 3
50+
@Test
51+
public void Test2() {
52+
TreeNode root = TreeNode.getBST2();
53+
int k1 = 1;
54+
int k2 = 2;
55+
ArrayList<Integer> actual = solution.searchRange(root, k1, k2);
56+
ArrayList<Integer> expected = new ArrayList<>();
57+
expected.add(1);
58+
expected.add(2);
59+
assertEquals(expected, actual);
60+
}
61+
62+
// 10
63+
// / \
64+
// 5 12
65+
// / \
66+
// 4 7
67+
@Test
68+
public void Test3() {
69+
TreeNode root = TreeNode.getBST5();
70+
int k1 = 6;
71+
int k2 = 13;
72+
ArrayList<Integer> actual = solution.searchRange(root, k1, k2);
73+
ArrayList<Integer> expected = new ArrayList<>();
74+
expected.add(7);
75+
expected.add(10);
76+
expected.add(12);
77+
assertEquals(expected, actual);
78+
}
79+
80+
// 2
81+
// / \
82+
// 1 3
83+
@Test
84+
public void Test4() {
85+
TreeNode root = TreeNode.getBST4();
86+
int k1 = 0;
87+
int k2 = 5;
88+
ArrayList<Integer> actual = solution.searchRange(root, k1, k2);
89+
ArrayList<Integer> expected = new ArrayList<>();
90+
expected.add(1);
91+
expected.add(2);
92+
expected.add(3);
93+
assertEquals(expected, actual);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy