首页 > 其他 > 详细

270. Closest Binary Search Tree Value

时间:2018-08-09 18:59:53      阅读:122      评论:0      收藏:0      [点我收藏+]
270. Closest Binary Search Tree Value

不停移动root直到走到底。 之间比较和当前最好的的值哪个更好。 注意先保存下 第一个root的value, 因为这个root 的 value 也有可能是 最好的值


class Solution {
    public int closestValue(TreeNode root, double target) {
        int result = root.val;
        while(root != null){
            if(Math.abs(root.val - target) < Math.abs(result - target)){
                result = root.val;
            }
            if(root.val > target){
                root = root.left;
            }else{
                root = root.right;
            }
        }
        return result;
        
    }
}

 

270. Closest Binary Search Tree Value

原文:https://www.cnblogs.com/tobeabetterpig/p/9450921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!