LeetCode 270. Closest Binary Search Tree Value(二叉搜索树最接近值查找)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

原题网址:https://leetcode.com/problems/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.

方法:递归查找。如果当前节点值小于目标值,则结果只可能是当前节点值或者右子树中的值;如果当前节点值大于目标值,则结果只可能是当前节点值或者左子树中的值。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int value;
    private void find(TreeNode root, double target) {
        if (Math.abs(root.val-target) < Math.abs(value-target)) value = root.val;
        if (root.val < target && root.right != null) find(root.right, target);
        if (root.val > target && root.left != null) find(root.left, target);
    }
    public int closestValue(TreeNode root, double target) {
        value = root.val;
        find(root, target);
        return value;
    }
}

优化:

/**
 * 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.val == target) return root.val;
        if (root.val < target) {
            if (root.right == null) return root.val;
            int right = closestValue(root.right, target);
            if (Math.abs(root.val-target) <= Math.abs(right-target)) return root.val;
            return right;
        } else {
            if (root.left == null) return root.val;
            int left = closestValue(root.left, target);
            if (Math.abs(root.val-target) <= Math.abs(left-target)) return root.val;
            return left;
        }
    }
}


算法-二叉搜索树中找接近的节点 问题: You are given a BST data structure consisting of BST nodes. Each BST node has an integer value stored in a property called "value" and two children nodes stored in properties called "left" and "right," respectively. A node is said to be a BST node i 阅读详情

相关推荐

LeetCode 270:在二叉搜索树中寻找接近(Swift 实战解析)

本文解析了 LeetCode270 题“Closest Binary Search Tree Value”,旨在帮助读者掌握在二叉搜索树(BST)中高效查找接近目标的技巧。通过利用 BST 的特性,代码从根节点开始,根据目标与当前节点的大小关系,决定向左子树还是右子树搜索,并记录接近的节点。文章提供了 Swift 语言的解题方案,并通过示例测试验证了代码的正确性。该方法的时间复杂度在平衡 BST 中为 O(log n),空间复杂度为 O(1),适用于需要快速查找接近某个的场景,如推荐系统或

记录从移动端、鸿蒙到物联网与AI的完整实战与思考。专注于分享可复用的工程方案与产品思维,尤其深入鸿蒙生态与全栈开发。以十年工程与管理沉淀,为你提供清晰的技术纵深路径。坚信技术的价值在于“贯通”与“落地”。 1119

LeetCode270接近二叉搜索树

1.题目描述 给定一个不为空的二叉搜索树和一个目标 target,请在该二叉搜索树中找到接近目标 target 的数。 注意: 给定的目标 target 是一个浮点数 题目保证在该二叉搜索树中只会存在一个接近目标的数 示例: 输入: root = [4,2,5,1,3],目标 target = 3.714286 4 / \ 2 5 / \ 1 ...

u011646912的博客 1244

leetcode270. 接近二叉搜索树

由于根节点是中间,在往下遍历时,根据目标和根节点的大小关系来比较,如果目标小于节点,则应该找更小的,于是到左子树去找,反之去右子树找。问题是如何利用二叉搜索树的特性来减少操作?......

OceanStar的博客 794

leetcode270. 接近二叉搜索树

给定一个不为空的二叉搜索树和一个目标 target,请在该二叉搜索树中找到接近目标 target 的数。 注意: 给定的目标 target 是一个浮点数 题目保证在该二叉搜索树中只会存在一个接近目标的数 示例: 输入: root = [4,2,5,1,3],目标 target = 3.714286 4 / \ 2 5 / \ 1 3 输出: 4...

少说,多做。 6804

900. 二叉搜索树接近  

900.二叉搜索树接近 给一棵非空二叉搜索树以及一个target,找到在BST中接近给定的节点 样例 样例1 输入: root = {5,4,9,2,#,8,10} and target = 6.124780 输出: 5 解释: 二叉树 {5,4,9,2,#,8,10},表示如下的树结构: 5 / \ 4 9 / / \ 2 8 10 样例2 输入: root = {3,2,4,1} and target = 4.142857 输出...

时代我西的博客 409

leetcode 270: Closest Binary Search Tree Value

Closest Binary Search Tree Value Total Accepted: 1815 Total Submissions: 6418 Difficulty: Easy Given a non-empty binary search tree and a target value, find the value in the BST that

西施豆腐渣 7634

LeetCode270. Closest Binary Search Tree Value 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归日期 题目地址:https://leetcode-cn.com/problems/closest-binary-search-tree-value/ 题目描述 Given a non-empty binary search tree a...

负雪明烛 1204

[leetcode] 270. Closest Binary Search Tree Value 解题报告

题目链接:https://leetcode.com/problems/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:

小榕流光的专栏 1467

LeetCode #270Closest 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...

LawFile的博客 259

Leetcode 270. Closest Binary Search Tree Value (cpp)

Leetcode 270. Closest Binary Search Tree Value (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 511

Leetcode NO.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

Programming my life 935

LeetCode 272. Closest Binary Search Tree Value II - 二叉搜索树Binary Search Tree)系列题7

​这题也是给定一个二叉搜索树和一个目标数,跟270. Closest Binary Search Tree Value不同的是,这要求找出树中k个接近目标数的节点。 关于求k个接近的数,很自然地想到用优先级队列。根据题意应该要用大堆的优先级队列,比较函数比较的是节点与目标差的绝对

hgq522的博客 411

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 o

330

LeetCode 270. Closest Binary Search Tree Value - 二叉搜索树Binary Search Tree)系列题6

题目给定一棵二叉搜索树和一个目标数,要求找到树中接近目标数的那个节点(两者差的绝对小)。 解法跟查找树中是否存在一个等于目标数的节点一样,基本思路就是利用二叉搜索树的特点,左子树所有节点都小于根节点,右子树所有节点都大于根节点

hgq522的博客 370

Leetcode270. Closest Binary Search Tree Value

题目地址: https://leetcode.com/problems/closest-binary-search-tree-value/ 给定一棵二叉搜索树,每个节点的都是整数,再给定一个浮点数target,问二叉搜索树中与target离的近的数是多少。 思路是二分法。如果target小于树根,显然答案只存在于左子树或者树根本身;如果target大于树根,显然答案只存在于右子树或者树根本身。...

数学、算法爱好者的博客 310

LeetCode 270. Closest Binary Search Tree Value - 二分查找Binary Search)系列题14

二叉搜索树查找一个数,如果不用二分查找法那就对不起二叉搜索树的名字。我们知道二叉搜索树的左子树的所有节点都比根节点小,右子树所有节点都比根节点大,因此很容易从根节点把树划分成两棵子树,通过判断可以排除掉一棵子树即减少一半的查找空间。

hgq522的博客 404

[leetCode]270. 接近二叉搜索树

题目 https://leetcode-cn.com/problems/closest-binary-search-tree-value/ 二分查找 根据root选选择在左子树中查找还是在右子树中查找 class Solution { public int closestValue(TreeNode root, double target) { int closest = root.val; while (root != null) {

Warms的博客 292

Leetcode日练笔记7 #270 Closest Binary Search Tree Value

#270Closest Binary Search Tree Value Given therootof a binary search tree and atargetvalue, returnthe value in the BST that is closest to thetarget. 解题思路: 本来一拿到题,完全没有头绪。 看example后。好像直接对root的list直接sorted,按diff从小到大排取小的就好。 但是忘了树节点和list的对应关系了。 I...

Bittersweet_t的博客 341

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 o

夜色之浓,莫过于黎明前的黑暗。 379
上一篇: LeetCode 269. Alien Dictionary(外星人字典)
下一篇: LeetCode 271. Encode and Decode Strings(字符串编解码)
jmspan
博客等级 码龄10年 99粉丝 431原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值