272. Closest Binary Search Tree Value II

Source: Internet
Author: User

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:

      1. Consider implement these the helper functions:
        1. getPredecessor(N), which returns the next smaller node to N.
        2. getSuccessor(N), which returns the next larger node to N.
      2. Try to assume this each node has a parent pointer, it makes the problem much easier.
      3. Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
      4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.