zoukankan      html  css  js  c++  java
  • LeetCode "Closest Binary Search Tree Value II"

    I made it too complicated first.. It is really simply if full-tree in-order traversal is allowed.

    class Solution 
    {
        int inx, sofar; // closest
        bool bleft; // target on left of closest
        vector<int> nums;
    
        void go(TreeNode *p, double target)
        {
            if (!p) return;
            go(p->left, target);
            nums.push_back(p->val);
            if (abs(sofar - target) > abs(p->val - target))
            {
                inx = nums.size() - 1;
                sofar = p->val;
                bleft = target < p->val;
            }
            go(p->right, target);
        }
    public:
        vector<int> closestKValues(TreeNode* root, double target, int k) 
        {
            sofar = std::numeric_limits<int>::min();
            go(root, target);
    
            size_t len = nums.size();
            int l = bleft ? inx - 1: inx;
            int r = bleft ? inx : inx + 1;
    
            vector<int> ret;
            while (k--)
            {
                if (l < 0)
                {
                    ret.push_back(nums[r ++]);
                }
                else if (r == len)
                {
                    ret.push_back(nums[l --]);
                }
                else
                {
                    double succ_diff = abs(nums[r] - target);
                    double pred_diff = abs(nums[l] - target);
                    if (succ_diff < pred_diff)
                        ret.push_back(nums[r++]);
                    else
                        ret.push_back(nums[l--]);
                }
            }
            return ret;
        }
    };
    View Code
  • 相关阅读:
    numpy操作
    python将整数均分成N等分
    Windows7下安装pyspark
    python的append insert extend pop del remove使用
    马尔科夫链
    dataframe行变换为列
    scala获取某个时间间隔的时间
    Python3+Flask+uwsgi部署
    A*寻路算法
    C++易混淆知识点整理
  • 原文地址:https://www.cnblogs.com/tonix/p/4778064.html
Copyright © 2011-2022 走看看