[LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
Note:
- Given target value is a floating point.
- You are guaranteed to have only one unique value in the BST that is closest to the target.
给一个非空二叉树和一个目标值,找到和目标值最接近的一个节点值。
利用二分搜索树的特点(左<根<右)来快速定位,由于根节点是中间值,在遍历时,如果目标值小于节点值,则找更小的值到左子树去找,反之去右子树找。
解法1:迭代
解法2:递归
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public int closestValue(TreeNode root, double target) { double min=Double.MAX_VALUE; int result = root.val; while (root!= null ){ if (target>root.val){ double diff = Math.abs(root.val-target); if (diff<min){ min = Math.min(min, diff); result = root.val; } root = root.right; } else if (target<root.val){ double diff = Math.abs(root.val-target); if (diff<min){ min = Math.min(min, diff); result = root.val; } root = root.left; } else { return root.val; } } return result; } |
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Solution { int goal; double min = Double.MAX_VALUE; public int closestValue(TreeNode root, double target) { helper(root, target); return goal; } public void helper(TreeNode root, double target){ if (root== null ) return ; if (Math.abs(root.val - target) < min){ min = Math.abs(root.val-target); goal = root.val; } if (target < root.val){ helper(root.left, target); } else { helper(root.right, target); } } } |
Java: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 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) { if (root == null ) return 0 ; int min = root.val; while (root != null ) { min = (Math.abs(root.val - target) < Math.abs(min - target) ? root.val : min); root = (root.val < target) ? root.right : root.left; } return min; } } |
Java: Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * 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 child = target < root.val ? root.left : root.right; if (child == null ) { return root.val; } int childClosest = closestValue(child, target); return Math.abs(root.val - target) < Math.abs(childClosest - target) ? root.val : childClosest; } } |
Python: Iteration, Time: O(h), Space: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution( object ): def closestValue( self , root, target): """ :type root: TreeNode :type target: float :rtype: int """ gap = float ( "inf" ) closest = float ( "inf" ) while root: if abs (root.val - target) < gap: gap = abs (root.val - target) closest = root if target = = root.val: break elif target < root.val: root = root.left else : root = root.right return closest.val |
C++: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public : int closestValue(TreeNode* root, double target) { double gap = numeric_limits< double >::max(); int closest = numeric_limits< int >::max(); while (root) { if ( abs ( static_cast < double >(root->val) - target) < gap) { gap = abs (root->val - target); closest = root->val; } if (target == root->val) { break ; } else if (target < root->val) { root = root->left; } else { root = root->right; } } return closest; } }; |
C++: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public : int closestValue(TreeNode* root, double target) { int res = root->val; while (root) { if ( abs (res - target) >= abs (root->val - target)) { res = root->val; } root = target < root->val ? root->left : root->right; } return res; } }; |
C++: Recursion
1 2 3 4 5 6 7 8 9 10 | class Solution { public : int closestValue(TreeNode* root, double target) { int a = root->val; TreeNode *t = target < a ? root->left : root->right; if (!t) return a; int b = closestValue(t, target); return abs (a - target) < abs (b - target) ? a : b; } }; |
C++: Recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution { public : int closestValue(TreeNode* root, double target) { int res = root->val; if (target < root->val && root->left) { int l = closestValue(root->left, target); if ( abs (res - target) >= abs (l - target)) res = l; } else if (target > root->val && root->right) { int r = closestValue(root->right, target); if ( abs (res - target) >= abs (r - target)) res = r; } return res; } }; |
类似题目:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构