Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite5b9fb7

Browse files
committed
03.02 (1) search range on BST using pruning
1 parent46b313b commite5b9fb7

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+
importjava.util.ArrayList;
20+
21+
importcom.leetcode.TreeNode;
22+
23+
/** see test {@link _02_SearchRangeInBinarySearchTree.SolutionTest } */
24+
publicclassSolution {
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+
publicArrayList<Integer>searchRange(TreeNoderoot,intk1,intk2) {
32+
ArrayList<Integer>res =newArrayList<>();
33+
if (root ==null) {
34+
returnres;
35+
}
36+
intval =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+
returnres;
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+
importstaticorg.junit.Assert.*;
4+
5+
importjava.util.ArrayList;
6+
7+
importorg.junit.After;
8+
importorg.junit.Before;
9+
importorg.junit.Rule;
10+
importorg.junit.Test;
11+
importorg.junit.rules.Timeout;
12+
13+
importcom.leetcode.TreeNode;
14+
15+
publicclassSolutionTest {
16+
17+
/** Test method for {@link _02_SearchRangeInBinarySearchTree.Solution } */
18+
Solutionsolution;
19+
20+
@Rule
21+
publicTimeoutglobalTimeout =newTimeout(200);
22+
23+
@Before
24+
publicvoidsetUp()throwsException {
25+
solution =newSolution();
26+
}
27+
28+
@After
29+
publicvoidtearDown()throwsException {
30+
solution =null;
31+
}
32+
33+
// 1
34+
@Test
35+
publicvoidTest1() {
36+
TreeNoderoot =TreeNode.getBST1();
37+
intk1 =0;
38+
intk2 =2;
39+
ArrayList<Integer>actual =solution.searchRange(root,k1,k2);
40+
ArrayList<Integer>expected =newArrayList<>();
41+
expected.add(1);
42+
assertEquals(expected,actual);
43+
}
44+
45+
// 1
46+
// \
47+
// 2
48+
// \
49+
// 3
50+
@Test
51+
publicvoidTest2() {
52+
TreeNoderoot =TreeNode.getBST2();
53+
intk1 =1;
54+
intk2 =2;
55+
ArrayList<Integer>actual =solution.searchRange(root,k1,k2);
56+
ArrayList<Integer>expected =newArrayList<>();
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+
publicvoidTest3() {
69+
TreeNoderoot =TreeNode.getBST5();
70+
intk1 =6;
71+
intk2 =13;
72+
ArrayList<Integer>actual =solution.searchRange(root,k1,k2);
73+
ArrayList<Integer>expected =newArrayList<>();
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+
publicvoidTest4() {
85+
TreeNoderoot =TreeNode.getBST4();
86+
intk1 =0;
87+
intk2 =5;
88+
ArrayList<Integer>actual =solution.searchRange(root,k1,k2);
89+
ArrayList<Integer>expected =newArrayList<>();
90+
expected.add(1);
91+
expected.add(2);
92+
expected.add(3);
93+
assertEquals(expected,actual);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp