Skip to content

Commit 4218aa9

Browse files
committed
二刷230
1 parent 42d8e7a commit 4218aa9

File tree

8 files changed

+116
-10
lines changed

8 files changed

+116
-10
lines changed

docs/0230-kth-smallest-element-in-a-bst.adoc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33

44
https://leetcode.com/problems/kth-smallest-element-in-a-bst/[LeetCode - Kth Smallest Element in a BST]
55

6-
TODO: 树的非递归遍历还需要多加推敲,加强理解。
7-
8-
image::images/0230-1.png[{image_attr}]
9-
10-
image::images/0230-2.png[{image_attr}]
11-
12-
image::images/0230-3.png[{image_attr}]
13-
14-
image::images/0230-4.png[{image_attr}]
15-
166
Given a binary search tree, write a function `kthSmallest` to find the *k*th smallest element in it.
177
188
*Note: *
@@ -55,10 +45,36 @@ You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
5545
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
5646

5747

48+
== 思路分析
49+
50+
二叉搜索树的中根遍历是排好序的,所以,求第 K 最小值,直接中根遍历即可。
51+
52+
树的非递归遍历还需要多加推敲,加强理解。
53+
54+
image::images/0230-05.png[{image_attr}]
55+
56+
image::images/0230-01.png[{image_attr}]
57+
58+
image::images/0230-02.png[{image_attr}]
59+
60+
image::images/0230-03.png[{image_attr}]
61+
62+
image::images/0230-04.png[{image_attr}]
5863

5964
[[src-0230]]
6065
[{java_src_attr}]
6166
----
6267
include::{sourcedir}/_0230_KthSmallestElementInABST.java[tag=answer]
6368
----
6469

70+
[[src-0230]]
71+
[{java_src_attr}]
72+
----
73+
include::{sourcedir}/_0230_KthSmallestElementInABst_2.java[tag=answer]
74+
----
75+
76+
== 参考资料
77+
78+
. https://leetcode.cn/problems/kth-smallest-element-in-a-bst/solutions/1050055/er-cha-sou-suo-shu-zhong-di-kxiao-de-yua-8o07/[230. 二叉搜索树中第K小的元素 - 官方题解^]
79+
. https://leetcode.cn/problems/kth-smallest-element-in-a-bst/solutions/2361685/230-er-cha-sou-suo-shu-zhong-di-k-xiao-d-n3he/[230. 二叉搜索树中第K小的元素 - 中序遍历,清晰图解^]
80+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/images/0230-05.png

186 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@
277277
|{doc_base_url}/0912-sort-an-array.adoc[题解]
278278
|归并,快排都可以
279279

280+
|{counter:codes}
281+
|{leetcode_base_url}/kth-smallest-element-in-a-bst/[230. Kth Smallest Element in a BST]
282+
|{doc_base_url}/0230-kth-smallest-element-in-a-bst.adoc[题解]
283+
|二叉树中根遍历
284+
280285
|===
281286

282287
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
/**
6+
* = 230. Kth Smallest Element in a BST
7+
*
8+
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/[Kth Smallest Element in a BST - LeetCode]
9+
*
10+
* Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
11+
*
12+
* Note:
13+
* You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
14+
*
15+
* .Example 1:
16+
* [source]
17+
* ----
18+
* Input: root = [3,1,4,null,2], k = 1
19+
* 3
20+
* / \
21+
* 1 4
22+
* \
23+
* 2
24+
* Output: 1
25+
* ----
26+
*
27+
* .Example 2:
28+
* [source]
29+
* ----
30+
* Input: root = [5,3,6,2,4,null,null,1], k = 3
31+
* 5
32+
* / \
33+
* 3 6
34+
* / \
35+
* 2 4
36+
* /
37+
* 1
38+
* Output: 3
39+
* ----
40+
*
41+
* *Follow up:*
42+
*
43+
* What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
44+
*
45+
* @author D瓜哥 · https://www.diguage.com
46+
* @since 2020-01-22 08:27
47+
*/
48+
public class _0230_KthSmallestElementInABst_2 {
49+
// tag::answer[]
50+
/**
51+
* Morris 中序遍历
52+
*
53+
* @author D瓜哥 · https://www.diguage.com
54+
* @since 2024-07-08 14:51:44
55+
*/
56+
public int kthSmallest(TreeNode root, int k) {
57+
TreeNode cur = root;
58+
TreeNode mostRight = null;
59+
TreeNode result = null;
60+
while (cur != null) {
61+
mostRight = cur.left;
62+
if (mostRight != null) {
63+
while (mostRight.right != null && mostRight.right != cur) {
64+
mostRight = mostRight.right;
65+
}
66+
if (mostRight.right == null) {
67+
mostRight.right = cur;
68+
cur = cur.left;
69+
continue;
70+
}else {
71+
mostRight.right = null;
72+
}
73+
}
74+
k--;
75+
if (k == 0) {
76+
result = cur;
77+
break;
78+
}
79+
System.out.println(cur.val);
80+
cur = cur.right;
81+
}
82+
return result.val;
83+
}
84+
// end::answer[]
85+
}

0 commit comments

Comments
 (0)