Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit adcd18b

Browse files
authored
Update 230._kth_smallest_element_in_a_bst.md
1 parent f9a6117 commit adcd18b

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

docs/Leetcode_Solutions/Python/230._kth_smallest_element_in_a_bst.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,49 @@
1-
### 230. Kth Smallest Element in a BST
1+
# 230. Kth Smallest Element in a BST
22

3-
题目:
4-
<https://leetcode.com/problems/kth-smallest-element-in-a-bst/>
3+
**<font color=red>难度: Medium</font>**
54

5+
## 刷题内容
66

7-
难度:
8-
Medium
7+
> 原题连接
8+
9+
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
10+
11+
> 内容描述
12+
13+
```
14+
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
15+
16+
Note:
17+
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
18+
19+
Example 1:
20+
21+
Input: root = [3,1,4,null,2], k = 1
22+
3
23+
/ \
24+
1 4
25+
\
26+
2
27+
Output: 1
28+
Example 2:
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+
Follow up:
40+
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?
41+
```
42+
43+
## 解题方案
44+
45+
> 思路 1
46+
******- 时间复杂度: O(N)******- 空间复杂度: O(N)******
947

1048

1149
跟昨天做的一道题类似,一上来就走取巧之路。
@@ -35,7 +73,7 @@ class Solution(object):
3573
```
3674

3775

38-
现在看到kth 就条件反射的想用divide & conquer, 扫root的左子树看nodes量,如果nodes数量是k-1,那么node就刚好是第k个,如果大于k > 左子树数量,扫右子树,同时更新root为root.right。
76+
现在看到kth 就条件反射的想用divide & conquer, 扫root的左子树看nodes量,如果nodes数量是k-1,那么root就刚好是第k个,如果大于k > 左子树数量,扫右子树,同时更新root为root.right。
3977

4078
看到的言论:
4179

0 commit comments

Comments
 (0)