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.
Follow up:
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
一刷
题解:
我们用两个栈,全部采用inorder访问树。
一个栈从小到大,存储<= target的节点
一个栈由大到小,存储>target的节点。
然后比较两个栈头的值,存储距离较小的点,一直进行k次。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
List<Integer> res = new ArrayList<>();
Stack<Integer> s1 = new Stack<>();//predecessors
Stack<Integer> s2 = new Stack<>();//successors
if(root == null) return res;
inorder(root, target, false, s1);//ascending, and store the node with value lower than or equal to target
inorder(root, target, true, s2);//descending
while(k-->0){
if(s1.isEmpty()) res.add(s2.pop());
else if(s2.isEmpty()) res.add(s1.pop());
else if(Math.abs(s1.peek() - target)<Math.abs(s2.peek() - target))
res.add(s1.pop());
else res.add(s2.pop());
}
return res;
}
private void inorder(TreeNode root, double target, boolean reverse, Stack<Integer> stack){
if(root == null) return;
inorder(reverse? root.right:root.left, target, reverse, stack);
// early terminate, no need to traverse the whole tree
if((reverse && root.val<=target) || (!reverse && root.val>target)) return;
// track the value of current node
stack.push(root.val);
inorder(reverse? root.left : root.right, target, reverse, stack);
}
}
二刷
题解:思路同上。但是我们这次用两个array和index来模拟上述行为。
inorder traverse,
如果root.val小于target, append在smaller尾部
否则,append在bigger尾部
所以smaller尾部和bigger的头部更满足要求。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
List<Integer> smaller = new ArrayList<>();
List<Integer> bigger = new ArrayList<>();
//inorder
inorder(root, target, smaller, bigger);
int sIndex = smaller.size()-1;
int bIndex = 0;
List<Integer> res = new ArrayList<>(k);
int index = 0;
while(res.size()<k){
if(sIndex<0){
res.add(bigger.get(bIndex++));
}else if(bIndex == bigger.size()){
res.add(smaller.get(sIndex--));
}else{
if(Math.abs(smaller.get(sIndex) - target) < Math.abs(bigger.get(bIndex) - target)){
res.add(smaller.get(sIndex--));
}else res.add(bigger.get(bIndex++));
}
}
return res;
}
private void inorder(TreeNode root, double target, List<Integer> smaller, List<Integer> bigger){
if(root == null) return;
inorder(root.left, target, smaller, bigger);
if(root.val<target) smaller.add(root.val);
else bigger.add(root.val);
inorder(root.right, target, smaller, bigger);
}
}