树
深度优先搜索
二叉搜索树
二分查找
二叉树
题目描述
给你二叉搜索树的根节点 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
解法
方法一:递归
我们定义一个递归函数 $\textit{dfs}(node)$,表示从当前节点 $node$ 开始,寻找最接近目标值 $target$ 的节点。我们可以通过比较当前节点的值与目标值的差的绝对值,来更新答案,如果目标值小于当前节点的值,我们就递归地搜索左子树,否则我们递归地搜索右子树。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉搜索树的节点数。
Python3 Java C++ Go TypeScript JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 # 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
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
32
33
34
35
36
37
38
39 /**
* 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 );
}
}
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
32 /**
* 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 ;
}
};
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 /**
* 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
}
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
32
33
34
35
36 /**
* 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 ;
}
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
32
33
34
35 /**
* 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 ;
};
方法二:迭代
我们可以将递归函数改写为迭代的形式,使用一个循环来模拟递归的过程。我们从根节点开始,判断当前节点的值与目标值的差的绝对值是否小于当前的最小差,如果是,我们就更新答案。然后根据目标值与当前节点的值的大小关系,决定向左子树还是右子树移动。当我们遍历到空节点时,循环结束。
时间复杂度 $O(n)$,其中 $n$ 是二叉搜索树的节点数。空间复杂度 $O(1)$。
Python3 Java C++ Go TypeScript JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 # 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
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 /**
* 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 ;
}
}
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.
* 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 ;
}
};
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 /**
* 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
}
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 /**
* 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 ;
}
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 /**
* 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 ;
};