44
44
45
45
<!-- 这里可写通用的实现逻辑 -->
46
46
47
+ ** 方法一:中序遍历**
48
+
49
+ 由于二叉搜索树的性质,中序遍历一定能得到升序序列,因此可以采用中序遍历找出第 k 小的元素。
50
+
47
51
<!-- tabs:start -->
48
52
49
53
### ** Python3**
59
63
# self.right = right
60
64
class Solution :
61
65
def kthSmallest (self , root : Optional[TreeNode], k : int ) -> int :
62
- def dfs (root ):
66
+ stk = []
67
+ while root or stk:
63
68
if root:
64
- nonlocal k, ans
65
- dfs(root.left)
69
+ stk.append(root)
70
+ root = root.left
71
+ else :
72
+ root = stk.pop()
66
73
k -= 1
67
74
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
75
77
```
76
78
77
79
### ** Java**
78
80
79
81
<!-- 这里可写当前语言的特殊实现逻辑 -->
80
82
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
-
121
83
``` java
122
84
/**
123
85
* Definition for a binary tree node.
@@ -136,35 +98,20 @@ class Solution {
136
98
*/
137
99
class Solution {
138
100
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;
148
106
} 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;
164
110
}
111
+ root = root. right;
165
112
}
166
113
}
167
- return ans ;
114
+ return 0 ;
168
115
}
169
116
}
170
117
```
@@ -185,24 +132,24 @@ class Solution {
185
132
*/
186
133
class Solution {
187
134
public:
188
- int k;
189
- int ans;
190
-
191
135
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())
201
138
{
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
+ }
204
151
}
205
- dfs(root->right) ;
152
+ return 0 ;
206
153
}
207
154
};
208
155
```
@@ -219,23 +166,22 @@ public:
219
166
* }
220
167
*/
221
168
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 {
226
171
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]
228
177
k--
229
178
if k == 0 {
230
- ans = root.Val
231
- return
179
+ return root.Val
232
180
}
233
- dfs( root.Right)
181
+ root = root .Right
234
182
}
235
183
}
236
-
237
- dfs(root)
238
- return ans
184
+ return 0
239
185
}
240
186
```
241
187
0 commit comments