剑指offer54 二叉搜索树的第k大节点【DFS】

题目描述

给定一棵二叉搜索树,请找出其中第k大的节点。

示例 1:

1
2
3
4
5
6
7
输入: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
输出: 4

示例 2:

1
2
3
4
5
6
7
8
9
输入: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
输出: 4

限制:

1 ≤ k ≤ 二叉搜索树元素个数

解题思路

方法一:深度优先搜索DFS

类似中序遍历思想,先遍历右子树后遍历左子树的,访问根节点时,对k进行减一操作,当k==0时,退出递归,设置res.

实现代码

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
class Solution54 {
int res = -1, n;

public int kthLargest(TreeNode root, int k) {
if (root == null || k == 0) {
return -1;
}
n = k;
dfs(root);
return res;
}

private void dfs(TreeNode root) {
if (root == null)
return;
dfs(root.right);
if (n == 0) {
return;
}
if (--n == 0) {
res = root.val;
return;
}
dfs(root.left);
}
}

复杂度分析

时间复杂度:O(n)

空间复杂度:O(n) // 递归栈空间

资料

剑指offer54 二叉搜索树的第k大节点【DFS】

http://example.com/2021/05/31/剑指offer54-二叉搜索树的第k大节点/

Author

John Doe

Posted on

2021-05-31

Updated on

2021-06-08

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.