FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc230.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc230.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (34 loc) · 870 Bytes
Breadcrumbs
leetcode
/
code
/
lc230.java
Copy path
File metadata and controls
37 lines (34 loc) · 870 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
27
28
29
30
31
32
33
34
35
36
37
package
code
;
import
java
.
util
.
Stack
;
/*
* 230. Kth Smallest Element in a BST
* 题意:二叉搜索树种第k小的数
* 难度:Medium
* 分类:Binary Search, Tree
* 思路:每次找到最小的值,k--。中序遍历。
* Tips:非递归中序遍历还是不熟练。。。
*/
public
class
lc230
{
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
) {
val
=
x
;
}
}
public
int
kthSmallest
(
TreeNode
root
,
int
k
) {
Stack
<
TreeNode
>
st
=
new
Stack
();
while
(!
st
.
isEmpty
()||
root
!=
null
){
while
(
root
!=
null
) {
st
.
add
(
root
);
root
=
root
.
left
;
}
root
=
st
.
pop
();
k
--;
if
(
k
==
0
)
return
root
.
val
;
root
=
root
.
right
;
}
return
0
;
}
}
Back
|
FazBrowse Home
|
New Git URL