码迷,mamicode.com
首页 > 其他好文 > 详细

270. Closest Binary Search Tree Value

时间:2016-06-27 06:46:35      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

    技术分享
/*
* 270. Closest Binary Search Tree Value * 2016-6-25 by Mingyang */ public int closestValue(TreeNode root, double target) { int closest = root.val; double min = Double.MAX_VALUE; while(root!=null) { if( Math.abs(root.val - target) < min ) { min = Math.abs(root.val - target); closest = root.val; } if(target < root.val) { root = root.left; } else if(target > root.val) { root = root.right; } else { return root.val; } } return closest; }

 

270. Closest Binary Search Tree Value

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5619022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!