Topic:
Given a non-empty binary search tree and a target value, find K values in the BST that is closest to the target.
Note:
- Given target value is a floating point.
- Assume k is always valid, which is: k ≤total nodes.
- You is guaranteed to a unique set of K values in the BST is closest to the target.
Follow up:
Assume that the BST are balanced, could you solve it in less than O(n) runtime (where n = Total nodes)?
Hint:
- Consider implement these the helper functions:
getPredecessor(N), which returns the next smaller node to N.
getSuccessor(N), which returns the next larger node to N.
- Try to assume this each node has a parent pointer, it makes the problem much easier.
- Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
- You would need-stacks to track the path in finding predecessor and successor node separately.
Links: http://leetcode.com/problems/closest-binary-search-tree-value-ii/
Exercises
At first the idea was very unclear, and a lot of discuss did not understand why. Think about it from the beginning at lunchtime, like closest Binary Search Tree Value I, the quest for the O (LOGN) solution can be difficult, but the O (n) solution should not be difficult to achieve. We can use the principle of in-order, starting with the leftmost element, maintaining a deque or doubly linked list, adding the value of this element from the back end to the deque, and then continuing through the next element. When the size of deque is K, the difference between the current element and the first element of the team and the target is compared to try to update the deque. The loop end condition is that the first element of the team is smaller than the target, or traverses the complete element. In this case, the time complexity is O (n), and the spatial complexity should be O (k).
Time Complexity-o (n), Space Complexity-o (k)
/*** Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode rig Ht * TreeNode (int x) {val = x;} }*/ Public classSolution { PublicList<integer> closestkvalues (TreeNode root,DoubleTargetintk) {LinkedList<Integer> res =NewLinkedlist<>(); Inorder (root, Target, K, res); returnRes; } Private voidInorder (TreeNode root,DoubleTargetintK, linkedlist<integer>Res) { if(Root = =NULL) { return; } inorder (Root.left, Target, K, res); if(res.size () = =k) {if(Math.Abs (res.get (0)-target) >= Math.Abs (Root.val-target)) {Res.removefirst (); Res.add (Root.val); } Else { return; } } Else{res.add (root.val); } inorder (Root.right, Target, K, res); }}
Reference:
Https://leetcode.com/discuss/55261/efficient-python
Https://leetcode.com/discuss/70577/java-in-order-traversal-1ms-solution
Https://leetcode.com/discuss/64713/clear-java-solution-with-one-stack-one-linkedlist
Https://leetcode.com/discuss/64713/clear-java-solution-with-one-stack-one-linkedlist
Https://leetcode.com/discuss/69220/2-ms-o-n-and-6-ms-o-logn-java-solution
Https://leetcode.com/discuss/55240/ac-clean-java-solution-using-two-stacks
Https://leetcode.com/discuss/55682/o-logn-java-solution-with-two-stacks-following-hint
Https://leetcode.com/discuss/55486/java-two-stacks-iterative-solution
Https://leetcode.com/discuss/64713/clear-java-solution-with-one-stack-one-linkedlist
Https://leetcode.com/discuss/71820/java-5ms-iterative-following-hint-o-klogn-time-and-space
Https://leetcode.com/discuss/70577/java-in-order-traversal-1ms-solution
272. Closest Binary Search Tree Value II