-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion230.java
More file actions
43 lines (40 loc) · 1.23 KB
/
Copy pathQuestion230.java
File metadata and controls
43 lines (40 loc) · 1.23 KB
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
40
41
42
43
import java.util.*;
public class Question230
{
public static void inorder(List<Integer> result,TreeNode root, int k)
{
if(result.size()<k)
{
if(root.left!=null)
inorder(result,root.left,k);
result.add(root.val);
if(root.right!=null)
inorder(result,root.right,k);
}
}
public static int kthSmallest(TreeNode root, int k)
{
List<Integer> result = new ArrayList<>();
inorder(result,root,k);
System.out.println(result);
return result.get(k-1);
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(10,null,null);
TreeNode l1 = new TreeNode(1,null,null);
TreeNode r1 = new TreeNode(3,null,null);
TreeNode ll2 = new TreeNode(-2,null,null);
TreeNode rr2 = new TreeNode(20,null,null);
TreeNode last = new TreeNode(33,null,null);
TreeNode lastnew = new TreeNode(123,null,null);
root.left = l1;
root.right = r1;
l1.left = ll2;
r1.right = rr2;
rr2.right = last;
last.left = lastnew;
Trees.display(root);
kthSmallest(root,5);
}
}