LeetCode-Closest Binary Search Tree Value II

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)?

Analysis:

Use inorder traverse, put all predecessors into a stack, for every successor, put all pres that has smaller gap than that successor into resList and then put this successor into resList.

Solution:

/**
 * 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) {
        Stack<Integer> pres = new Stack<Integer>();
        LinkedList<Integer> resList = new LinkedList<Integer>();
        closestKValuesRecur(root,target,k,pres,resList);
        // If not enough in resList, put more pres into resList. This is because successor is too little.
        while (resList.size()<k && !pres.empty()){
                resList.addFirst(pres.pop());
        }
        return resList;
    }

    public void closestKValuesRecur(TreeNode curNode, double target, int k, Stack<Integer> pres, LinkedList<Integer> resList){
        if (curNode == null) return;
        if (resList.size()==k) return;

        // inorder traverse.
        closestKValuesRecur(curNode.left,target,k,pres,resList);

        // check curNode
        if (curNode.val >= target){
            while (resList.size()<k && !pres.empty() && target-pres.peek() < curNode.val-target){
                resList.addFirst(pres.pop());
            }
            if (resList.size()<k){
                resList.addLast(curNode.val);
            } else {
                return;
            }
        } else {
            pres.push(curNode.val);
        }

        closestKValuesRecur(curNode.right,target,k,pres,resList);
    }
}
时间: 2024-12-31 03:12:31

LeetCode-Closest Binary Search Tree Value II的相关文章

[LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

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 onl

LeetCode Closest Binary Search Tree Value

原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed

272. Closest Binary Search Tree Value II

/* * 272. Closest Binary Search Tree Value II * 2016-6-25 by Mingyang * 一开始这个题目我想用priority queue来做,就是把所有的stack里面的全部加进来 * 然后一个一个的找 */ public List<Integer> closestKValues1(TreeNode root, double target, int k) { Stack<TreeNode> stack = new Stack&

[LeetCode#272] Closest Binary Search Tree Value II

Problem: 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

[?]*Closest Binary Search Tree Value II

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 onl

[LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed to have only one unique value in the BST that is closest to the target.

LC 272. Closest Binary Search Tree Value II

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 onl

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s

LeetCode :: Validate Binary Search Tree[详细分析]

Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also b