列表

详情


270. 最接近的二叉搜索树值

给你二叉搜索树的根节点 root 和一个目标值 target ,请在该二叉搜索树中找到最接近目标值 target 的数值。如果有多个答案,返回最小的那个。

 

示例 1:

输入:root = [4,2,5,1,3], target = 3.714286
输出:4

示例 2:

输入:root = [1], target = 4.428571
输出:1

 

提示:

相似题目

完全二叉树的节点个数

最接近的二叉搜索树值 II

二叉搜索树中的搜索

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int closestValue(TreeNode* root, double target) { } };

golang 解法, 执行用时: 4 ms, 内存消耗: 4.7 MB, 提交时间: 2023-10-15 19:17:47

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func closestValue(root *TreeNode, target float64) int {
    var dfs func(*TreeNode,float64)
    ans:=0
    minValue:=math.MaxFloat64
    dfs = func(node *TreeNode,target float64){
        if node==nil{
            return 
        }
        dfs(node.Left,target)
        nodeValue:=float64(node.Val)
        if abs(nodeValue-target)<minValue{
            minValue = abs(nodeValue-target)
            ans = node.Val
        }
        dfs(node.Right,target)
    }
    dfs(root,target)
    return ans
}

func min(a,b float64) float64{
    if a<b{
        return a
    }
    return b
}

func abs(x float64) float64{
    if x<0{
        return -x
    }
    return x
}

cpp 解法, 执行用时: 8 ms, 内存消耗: 20.9 MB, 提交时间: 2023-10-15 19:17:20

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int closestValue(TreeNode* root, double target) {
        int res = root->val;
        while (root != nullptr) {
            if (abs(target - root->val) < abs(target - res))
                res = root->val;
            else if (abs(target - root->val) == abs(target - res))
                res = min(res, root->val);
            root = target > root->val ? root->right : root->left;
        }
        return res;
    }
};

java 解法, 执行用时: 0 ms, 内存消耗: 43 MB, 提交时间: 2023-10-15 19:17:08

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int closestValue(TreeNode root, double target) {
        int res = root.val;
        while (root != null) {
            if (Math.abs(target - root.val) < Math.abs(target - res))
                res = root.val;
            else if (Math.abs(target - root.val) == Math.abs(target - res))
                res = Math.min(res, root.val);
            root = target > root.val ? root.right : root.left;
        }
        return res;
    }
}

python3 解法, 执行用时: 48 ms, 内存消耗: 18.5 MB, 提交时间: 2023-10-15 19:16:41

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def closestValue(self, root: Optional[TreeNode], target: float) -> int:
        node = root
        res = root.val
        has_change = True
        while has_change:
            has_change = False
            res = min(res, node.val,key=lambda x:(abs(x - target), x))
            if node.left is not None and target < node.val:
                node = node.left
                has_change = True
            elif node.right is not None and target > node.val:
                node = node.right
                has_change = True
        return res

上一题