[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.
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值