-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprblm230.java
More file actions
31 lines (24 loc) · 762 Bytes
/
prblm230.java
File metadata and controls
31 lines (24 loc) · 762 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
import java.util.*;
public class prblm230 {
List<Integer> list = new ArrayList<>();
public static void main(String[] args) {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(6);
root.left.left = new TreeNode(2);
root.left.right = new TreeNode(4);
root.left.left.left = new TreeNode(1);
System.out.println(new prblm230().kthSmallest(root, 3));
}
public int kthSmallest(TreeNode root, int k) {
dfs(root);
Collections.sort(list);
return list.get(k - 1);
}
private void dfs(TreeNode root){
if(root == null) return;
list.add(root.val);
dfs(root.left);
dfs(root.right);
}
}