-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode230.java
More file actions
39 lines (33 loc) · 941 Bytes
/
LeetCode230.java
File metadata and controls
39 lines (33 loc) · 941 Bytes
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
38
39
import util.TreeNode;
public class LeetCode230 {
public static void main(String[] args) {
// 输入:root = [3,1,4,null,2], k = 1
// 输出:1
System.out.println(new Solution230().kthSmallest(TreeNode.buildTree(new Integer[] { 3, 1, 4, null, 2 }), 1));
// 输入:root = [5,3,6,2,4,null,null,1], k = 3
// 输出:3
System.out.println(
new Solution230().kthSmallest(TreeNode.buildTree(new Integer[] { 5, 3, 6, 2,
4, null, null, 1 }), 3));
}
}
class Solution230 {
int result;
int k;
public int kthSmallest(TreeNode root, int k) {
this.k = k;
dfs(root);
return result;
}
public void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
k--;
if (k == 0) {
result = root.val;
}
dfs(root.right);
}
}