Skip to content

Commit a23e7f1

Browse files
refactor 272
1 parent 60dee92 commit a23e7f1

File tree

1 file changed

+37
-34
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-34
lines changed

src/main/java/com/fishercoder/solutions/_272.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.Stack;
88

99
/**
10+
* 272. Closest Binary Search Tree Value II
11+
*
1012
* Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
1113
1214
Note:
@@ -28,44 +30,45 @@ Assume that the BST is balanced, could you solve it in less than O(n) runtime (w
2830
*/
2931
public class _272 {
3032

31-
public List<Integer> closestKValues(TreeNode root, double target, int k) {
32-
List<Integer> res = new ArrayList();
33+
public static class Solution1 {
34+
public List<Integer> closestKValues(TreeNode root, double target, int k) {
35+
List<Integer> res = new ArrayList();
3336

34-
Stack<Integer> s1 = new Stack(); // predecessors
35-
Stack<Integer> s2 = new Stack(); // successors
37+
Stack<Integer> s1 = new Stack(); // predecessors
38+
Stack<Integer> s2 = new Stack(); // successors
3639

37-
inorder(root, target, false, s1);
38-
inorder(root, target, true, s2);
40+
inorder(root, target, false, s1);
41+
inorder(root, target, true, s2);
3942

40-
while (k-- > 0) {
41-
if (s1.isEmpty()) {
42-
res.add(s2.pop());
43-
} else if (s2.isEmpty()) {
44-
res.add(s1.pop());
45-
} else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target)) {
46-
res.add(s1.pop());
47-
} else {
48-
res.add(s2.pop());
49-
}
50-
}
43+
while (k-- > 0) {
44+
if (s1.isEmpty()) {
45+
res.add(s2.pop());
46+
} else if (s2.isEmpty()) {
47+
res.add(s1.pop());
48+
} else if (Math.abs(s1.peek() - target) < Math.abs(s2.peek() - target)) {
49+
res.add(s1.pop());
50+
} else {
51+
res.add(s2.pop());
52+
}
53+
}
5154

52-
return res;
53-
}
55+
return res;
56+
}
5457

55-
// inorder traversal
56-
void inorder(TreeNode root, double target, boolean reverse, Stack<Integer> stack) {
57-
if (root == null) {
58-
return;
59-
}
60-
61-
inorder(reverse ? root.right : root.left, target, reverse, stack);
62-
// early terminate, no need to traverse the whole tree
63-
if ((reverse && root.val <= target) || (!reverse && root.val > target)) {
64-
return;
65-
}
66-
// track the value of current node
67-
stack.push(root.val);
68-
inorder(reverse ? root.left : root.right, target, reverse, stack);
69-
}
58+
// inorder traversal
59+
void inorder(TreeNode root, double target, boolean reverse, Stack<Integer> stack) {
60+
if (root == null) {
61+
return;
62+
}
7063

64+
inorder(reverse ? root.right : root.left, target, reverse, stack);
65+
// early terminate, no need to traverse the whole tree
66+
if ((reverse && root.val <= target) || (!reverse && root.val > target)) {
67+
return;
68+
}
69+
// track the value of current node
70+
stack.push(root.val);
71+
inorder(reverse ? root.left : root.right, target, reverse, stack);
72+
}
73+
}
7174
}

0 commit comments

Comments
 (0)