comments | difficulty | edit_url | tags | |||||
---|---|---|---|---|---|---|---|---|
true |
简单 |
|
给你二叉搜索树的根节点 root
和一个目标值 target
,请在该二叉搜索树中找到最接近目标值 target
的数值。如果有多个答案,返回最小的那个。
示例 1:
输入:root = [4,2,5,1,3], target = 3.714286 输出:4
示例 2:
输入:root = [1], target = 4.428571 输出:1
提示:
- 树中节点的数目在范围
[1, 104]
内 0 <= Node.val <= 109
-109 <= target <= 109
我们定义一个递归函数
时间复杂度
# 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:
def dfs(node: Optional[TreeNode]):
if node is None:
return
nxt = abs(target - node.val)
nonlocal ans, diff
if nxt < diff or (nxt == diff and node.val < ans):
diff = nxt
ans = node.val
node = node.left if target < node.val else node.right
dfs(node)
ans = 0
diff = inf
dfs(root)
return ans
/**
* 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 {
private int ans;
private double target;
private double diff = Double.MAX_VALUE;
public int closestValue(TreeNode root, double target) {
this.target = target;
dfs(root);
return ans;
}
private void dfs(TreeNode node) {
if (node == null) {
return;
}
double nxt = Math.abs(node.val - target);
if (nxt < diff || (nxt == diff && node.val < ans)) {
diff = nxt;
ans = node.val;
}
node = target < node.val ? node.left : node.right;
dfs(node);
}
}
/**
* 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 ans = root->val;
double diff = INT_MAX;
function<void(TreeNode*)> dfs = [&](TreeNode* node) {
if (!node) {
return;
}
double nxt = abs(node->val - target);
if (nxt < diff || (nxt == diff && node->val < ans)) {
diff = nxt;
ans = node->val;
}
node = target < node->val ? node->left : node->right;
dfs(node);
};
dfs(root);
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func closestValue(root *TreeNode, target float64) int {
ans := root.Val
diff := math.MaxFloat64
var dfs func(*TreeNode)
dfs = func(node *TreeNode) {
if node == nil {
return
}
nxt := math.Abs(float64(node.Val) - target)
if nxt < diff || (nxt == diff && node.Val < ans) {
diff = nxt
ans = node.Val
}
if target < float64(node.Val) {
dfs(node.Left)
} else {
dfs(node.Right)
}
}
dfs(root)
return ans
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function closestValue(root: TreeNode | null, target: number): number {
let ans = 0;
let diff = Number.POSITIVE_INFINITY;
const dfs = (node: TreeNode | null): void => {
if (!node) {
return;
}
const nxt = Math.abs(target - node.val);
if (nxt < diff || (nxt === diff && node.val < ans)) {
diff = nxt;
ans = node.val;
}
node = target < node.val ? node.left : node.right;
dfs(node);
};
dfs(root);
return ans;
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {number}
*/
var closestValue = function (root, target) {
let ans = 0;
let diff = Infinity;
const dfs = node => {
if (!node) {
return;
}
const nxt = Math.abs(target - node.val);
if (nxt < diff || (nxt === diff && node.val < ans)) {
diff = nxt;
ans = node.val;
}
node = target < node.val ? node.left : node.right;
dfs(node);
};
dfs(root);
return ans;
};
我们可以将递归函数改写为迭代的形式,使用一个循环来模拟递归的过程。我们从根节点开始,判断当前节点的值与目标值的差的绝对值是否小于当前的最小差,如果是,我们就更新答案。然后根据目标值与当前节点的值的大小关系,决定向左子树还是右子树移动。当我们遍历到空节点时,循环结束。
时间复杂度
# 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:
ans, diff = root.val, inf
while root:
nxt = abs(root.val - target)
if nxt < diff or (nxt == diff and root.val < ans):
diff = nxt
ans = root.val
root = root.left if target < root.val else root.right
return ans
/**
* 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 ans = root.val;
double diff = Double.MAX_VALUE;
while (root != null) {
double nxt = Math.abs(root.val - target);
if (nxt < diff || (nxt == diff && root.val < ans)) {
diff = nxt;
ans = root.val;
}
root = target < root.val ? root.left : root.right;
}
return ans;
}
}
/**
* 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 ans = root->val;
double diff = INT_MAX;
while (root) {
double nxt = abs(root->val - target);
if (nxt < diff || (nxt == diff && root->val < ans)) {
diff = nxt;
ans = root->val;
}
root = target < root->val ? root->left : root->right;
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func closestValue(root *TreeNode, target float64) int {
ans := root.Val
diff := math.MaxFloat64
for root != nil {
nxt := math.Abs(float64(root.Val) - target)
if nxt < diff || (nxt == diff && root.Val < ans) {
diff = nxt
ans = root.Val
}
if float64(root.Val) > target {
root = root.Left
} else {
root = root.Right
}
}
return ans
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function closestValue(root: TreeNode | null, target: number): number {
let ans = 0;
let diff = Number.POSITIVE_INFINITY;
while (root) {
const nxt = Math.abs(root.val - target);
if (nxt < diff || (nxt === diff && root.val < ans)) {
diff = nxt;
ans = root.val;
}
root = target < root.val ? root.left : root.right;
}
return ans;
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} target
* @return {number}
*/
var closestValue = function (root, target) {
let ans = root.val;
let diff = Infinity;
while (root) {
const nxt = Math.abs(root.val - target);
if (nxt < diff || (nxt === diff && root.val < ans)) {
diff = nxt;
ans = root.val;
}
root = target < root.val ? root.left : root.right;
}
return ans;
};