Closest Binary Search Tree Value
来源:互联网 发布:手机数据开了不能用 编辑:程序博客网 时间:2024/05/20 21:22
这道题让自己有种开发智力的赶脚,请再好好看看,尤其是第一种递归解法。
有两种解法。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int closestValue(TreeNode root, double target) { TreeNode kid = null; if (root.val > target) { kid = root.left; } else { kid = root.right; } if (kid == null) { return root.val; } int closest = closestValue(kid, target); return Math.abs(root.val - target) < Math.abs(closest - target) ? root.val : closest; } // public int closestValue(TreeNode root, double target) { // //int closest = 0; // int closest = root.val; // while (root != null) { // closest = Math.abs(root.val - target) < Math.abs(closest - target) ? root.val : closest; // root = root.val > target ? root.left : root.right; // } // return closest; // }}
0 0
- Closest Binary Search Tree Value
- Closest Binary Search Tree Value
- Closest Value in Binary Search Tree
- [Leetcode]Closest Binary Search Tree Value II
- [Leetcode]Closest Binary Search Tree Value
- leetcode 270: Closest Binary Search Tree Value
- *LeetCode-Closest Binary Search Tree Value
- [leetcode 270] Closest Binary Search Tree Value
- [LeetCode270]Closest Binary Search Tree Value
- [LeetCode282]Closest Binary Search Tree Value II
- Leetcode 270Closest Binary Search Tree Value
- 270: Closest Binary Search Tree Value
- LeetCode 270: Closest Binary Search Tree Value
- Closest Binary Search Tree Value II
- Closest Binary Search Tree Value II
- LeetCode 270. Closest Binary Search Tree Value
- 270. Closest Binary Search Tree Value
- 272. Closest Binary Search Tree Value II
- SpringMVC之安全性(二)登录界面
- SpringMVC之安全性(三)Twitter登入
- SpringMVC小结(四)
- 结束SpringMVC
- 成为C++高手之头文件与条件编译
- Closest Binary Search Tree Value
- MySQL读取不重复的数据
- [最大密度子图 最小割] BZOJ 1312 Neerc2006 Hard Life
- Strobogrammatic Number
- [DP Euler Zigzag Number] BZOJ 1925 [Sdoi2010]地精部落
- [组合数学] BZOJ 2467 [中山市选2010]生成树
- [高斯消元 线性基 生成树 随机化权值Xor] BZOJ 3569 DZY Loves Chinese II
- [分治] BZOJ 3745 [Coci2015]Norma
- Android Service基础