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.
Example
No.1
Input:
{1}
0.000000
1
Output:
[1]
Explanation:
Binary tree {1}, denote the following structure:
1 | 1 |
No.2
Input:
{3,1,4,#,2}
0.275000
2
Output:
[1,2]
Explanation:
Binary tree {3,1,4,#,2}, denote the following structure:
1 | 3 |
Challenge
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
Code
1 | public class TreeNode { |
1 | private Stack<TreeNode> predecessor = new Stack<>(); |