forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClosestBinarySearchTreeValueII.java
136 lines (118 loc) · 3.77 KB
/
ClosestBinarySearchTreeValueII.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
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
package leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : ClosestBinarySearchTreeValueII
* Creator : Edward
* Date : Dec, 2017
* Description : 272. Closest Binary Search Tree Value II
*/
public class ClosestBinarySearchTreeValueII {
/**
* Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
Note:
Given target value is a floating point.
You may assume k is always valid, that is: k ≤ total nodes.
You are guaranteed to have only one unique set of k values in the BST that are closest to the target.
8
/ \
3 9
/ \
2 7
target : 6 k = 2
Stack : pred : 3
Stack : succ : 9
3 1
res : 7 8
* @param root
* @param target
* @param k
* @return
*/
// time : O(n) space : O(n)
public List<Integer> closestKValues(TreeNode root, double target, int k) {
LinkedList<Integer> res = new LinkedList<>();
helper(res, root, target, k);
return res;
}
private void helper(LinkedList<Integer> res, TreeNode root, double target, int k) {
if (root == null) return;
helper(res, root.left, target, k);
if (res.size() == k) {
if (Math.abs(target - root.val) < Math.abs(target - res.peekFirst())) {
res.removeFirst();
} else return;
}
res.add(root.val);
helper(res, root.right, target, k);
}
// time : O(klogn)
public List<Integer> closestKValues2(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> pred = new Stack<>();
Stack<TreeNode> succ = new Stack<>();
iniPred(root, target, pred);
iniSucc(root, target, succ);
if (!pred.isEmpty() && !succ.isEmpty() && succ.peek().val == pred.peek().val) {
helper(pred, false);
}
while (k-- > 0) {
if (succ.isEmpty()) {
res.add(helper(pred, false));
} else if (pred.isEmpty()) {
res.add(helper(succ, true));
} else {
double succDiff = Math.abs((double)succ.peek().val - target);
double predDiff = Math.abs((double)pred.peek().val - target);
if (succDiff < predDiff) {
res.add(helper(succ, true));
} else {
res.add(helper(pred, false));
}
}
}
return res;
}
private void iniSucc(TreeNode root, double target, Stack<TreeNode> succ) {
while (root != null) {
if (root.val == target) {
succ.push(root);
break;
} else if (root.val > target) {
succ.push(root);
root = root.left;
} else {
root = root.right;
}
}
}
private void iniPred(TreeNode root, double target, Stack<TreeNode> pred) {
while (root != null) {
if (root.val == target) {
pred.push(root);
break;
} else if (root.val < target) {
pred.push(root);
root = root.right;
} else {
root = root.left;
}
}
}
private int helper(Stack<TreeNode> stack, boolean isSucc) {
TreeNode cur = stack.pop();
int res = cur.val;
if (isSucc) cur = cur.right;
else cur = cur.left;
while (cur != null) {
stack.push(cur);
if (isSucc) cur = cur.left;
else cur = cur.right;
}
return res;
}
}