[LeetCode282]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)?

Hint:

    *Consider implement these two 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 that 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 two stacks to track the path in finding predecessor and successor node separately.
Hide Company Tags Google
Hide Tags Tree Stack
Hide Similar Problems (M) Binary Tree Inorder Traversal (E) Closest Binary Search Tree Value

对于我这种脑子迟钝的人,需要请出一个好朋友——实际的例子:
一个balanced binary search tree.
preorder sequence: 14, 21, 24, 25, 28, 31, 32, 36, 39, 41, 47
preorder sequence: 14, 21, 24, 25, 28, 31, 32, 36, 39, 41, 47
target = 26.00, k = 3

如果要less than O(n)的方法,我们可以用两个stack 根据target的大小装入小于等于target的一半和大于target的一半。对于本例,
stk1: 14, 21, 24, 25
stk2:28, 31, 32, 36, 39, 41, 47;

如果直接这么载入,stk2那部分很难用,需要reverse一下,让stk2变成:stk2: 47, 41, 39, 36, 32, 31, 28

有了这两个stk就可以找到3个离26.0最近的点了:24, 25, 28.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> closestKValues(TreeNode* root, double target, int k) {
        stack<int> predecessor;
        stack<int> successor;
        vector<int> res;
        if(!root) return res;
        inorderTraverse(root, target, false, predecessor);
        inorderTraverse(root, target, true, successor);
        while(k){
            if(predecessor.empty()){
                res.push_back(successor.top());
                successor.pop();
            }else if(successor.empty()){
                res.push_back(predecessor.top());
                predecessor.pop();
            }else if(abs(predecessor.top() - target) < abs(successor.top() - target)){
                res.push_back(predecessor.top());
                predecessor.pop();
            }else{
                res.push_back(successor.top());
                successor.pop();
            }
            --k;
        }
        return res;
    }
    void inorderTraverse(TreeNode* node, double target, bool reverse, stack<int>& stk){
        if(!node) return;
        inorderTraverse(reverse ? node->right : node->left, target, reverse, stk); // inorder traverse: left, root, right;
        if((!reverse && node->val > target) || ( reverse && node->val <= target)) return;
        stk.push(node->val);
        inorderTraverse(reverse ? node->left : node->right, target, reverse, stk);// right.
    }
};
发布了109 篇原创文章 · 获赞 0 · 访问量 4万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章