首页 > 其他 > 详细

*Closest Binary Search Tree Value

时间:2016-01-06 06:48:18      阅读:214      评论:0      收藏:0      [点我收藏+]

 

 

 

Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.

 

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

 

*Closest Binary Search Tree Value

原文:http://www.cnblogs.com/hygeia/p/5104289.html

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