Skip to content

Commit 36f39e1

Browse files
committed
feat: update solutions to lc problem: No.0230
No.0230.Kth Smallest Element in a BST
1 parent 0baa195 commit 36f39e1

File tree

7 files changed

+143
-262
lines changed

7 files changed

+143
-262
lines changed

.github/workflows/sync.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ jobs:
1818
source-repo: git@github.com:doocs/leetcode.git
1919
destination-repo: git@gitee.com:Doocs/leetcode.git
2020

21-
- name: Build Gitee Pages
22-
uses: yanglbme/gitee-pages-action@main
23-
with:
24-
gitee-username: yanglbme
25-
gitee-password: ${{ secrets.GITEE_PASSWORD }}
26-
gitee-repo: Doocs/leetcode
27-
branch: main
21+
# - name: Build Gitee Pages
22+
# uses: yanglbme/gitee-pages-action@main
23+
# with:
24+
# gitee-username: yanglbme
25+
# gitee-password: ${{ secrets.GITEE_PASSWORD }}
26+
# gitee-repo: Doocs/leetcode
27+
# branch: main

solution/0200-0299/0230.Kth Smallest Element in a BST/README.md

+47-101
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:中序遍历**
48+
49+
由于二叉搜索树的性质,中序遍历一定能得到升序序列,因此可以采用中序遍历找出第 k 小的元素。
50+
4751
<!-- tabs:start -->
4852

4953
### **Python3**
@@ -59,65 +63,23 @@
5963
# self.right = right
6064
class Solution:
6165
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
62-
def dfs(root):
66+
stk = []
67+
while root or stk:
6368
if root:
64-
nonlocal k, ans
65-
dfs(root.left)
69+
stk.append(root)
70+
root = root.left
71+
else:
72+
root = stk.pop()
6673
k -= 1
6774
if k == 0:
68-
ans = root.val
69-
return
70-
dfs(root.right)
71-
72-
ans = -1
73-
dfs(root)
74-
return ans
75+
return root.val
76+
root = root.right
7577
```
7678

7779
### **Java**
7880

7981
<!-- 这里可写当前语言的特殊实现逻辑 -->
8082

81-
```java
82-
/**
83-
* Definition for a binary tree node.
84-
* public class TreeNode {
85-
* int val;
86-
* TreeNode left;
87-
* TreeNode right;
88-
* TreeNode() {}
89-
* TreeNode(int val) { this.val = val; }
90-
* TreeNode(int val, TreeNode left, TreeNode right) {
91-
* this.val = val;
92-
* this.left = left;
93-
* this.right = right;
94-
* }
95-
* }
96-
*/
97-
class Solution {
98-
private int k;
99-
private int ans;
100-
101-
public int kthSmallest(TreeNode root, int k) {
102-
this.k = k;
103-
dfs(root);
104-
return ans;
105-
}
106-
107-
private void dfs(TreeNode root) {
108-
if (root == null) {
109-
return;
110-
}
111-
dfs(root.left);
112-
if (--k == 0) {
113-
ans = root.val;
114-
return;
115-
}
116-
dfs(root.right);
117-
}
118-
}
119-
```
120-
12183
```java
12284
/**
12385
* Definition for a binary tree node.
@@ -136,35 +98,20 @@ class Solution {
13698
*/
13799
class Solution {
138100
public int kthSmallest(TreeNode root, int k) {
139-
int ans = -1;
140-
while (root != null) {
141-
if (root.left == null) {
142-
--k;
143-
if (k == 0) {
144-
ans = root.val;
145-
return ans;
146-
}
147-
root = root.right;
101+
Deque<TreeNode> stk = new ArrayDeque<>();
102+
while (root != null || !stk.isEmpty()) {
103+
if (root != null) {
104+
stk.push(root);
105+
root = root.left;
148106
} else {
149-
TreeNode pre = root.left;
150-
while (pre.right != null && pre.right != root) {
151-
pre = pre.right;
152-
}
153-
if (pre.right == null) {
154-
pre.right = root;
155-
root = root.left;
156-
} else {
157-
--k;
158-
if (k == 0) {
159-
ans = root.val;
160-
return ans;
161-
}
162-
pre.right = null;
163-
root = root.right;
107+
root = stk.pop();
108+
if (--k == 0) {
109+
return root.val;
164110
}
111+
root = root.right;
165112
}
166113
}
167-
return ans;
114+
return 0;
168115
}
169116
}
170117
```
@@ -185,24 +132,24 @@ class Solution {
185132
*/
186133
class Solution {
187134
public:
188-
int k;
189-
int ans;
190-
191135
int kthSmallest(TreeNode* root, int k) {
192-
this->k = k;
193-
dfs(root);
194-
return ans;
195-
}
196-
197-
void dfs(TreeNode* root) {
198-
if (!root) return;
199-
dfs(root->left);
200-
if (--k == 0)
136+
stack<TreeNode*> stk;
137+
while (root || !stk.empty())
201138
{
202-
ans = root->val;
203-
return;
139+
if (root)
140+
{
141+
stk.push(root);
142+
root = root->left;
143+
}
144+
else
145+
{
146+
root = stk.top();
147+
stk.pop();
148+
if (--k == 0) return root->val;
149+
root = root->right;
150+
}
204151
}
205-
dfs(root->right);
152+
return 0;
206153
}
207154
};
208155
```
@@ -219,23 +166,22 @@ public:
219166
* }
220167
*/
221168
func kthSmallest(root *TreeNode, k int) int {
222-
var ans int
223-
224-
var dfs func(root *TreeNode)
225-
dfs = func(root *TreeNode) {
169+
stk := []*TreeNode{}
170+
for root != nil || len(stk) > 0 {
226171
if root != nil {
227-
dfs(root.Left)
172+
stk = append(stk, root)
173+
root = root.Left
174+
} else {
175+
root = stk[len(stk)-1]
176+
stk = stk[:len(stk)-1]
228177
k--
229178
if k == 0 {
230-
ans = root.Val
231-
return
179+
return root.Val
232180
}
233-
dfs(root.Right)
181+
root = root.Right
234182
}
235183
}
236-
237-
dfs(root)
238-
return ans
184+
return 0
239185
}
240186
```
241187

0 commit comments

Comments
 (0)