-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode0230.java
More file actions
66 lines (58 loc) · 1.66 KB
/
Copy pathLeetCode0230.java
File metadata and controls
66 lines (58 loc) · 1.66 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Kth Smallest Element in a BST
* Input: k = 3,
* 5
/ \
3 6
/ \
2 4
/
1
* Output: 3
* */
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class LeetCode0230 {
public static int index = 0;
public static int[] TREE_VALUE = new int[]{5, 3, 2, 1, 0, 0, 0, 4, 0, 0, 6, 0, 0};
public static void main(String args[]) {
TreeNode root = new TreeNode();
root = TreeNode.createTree(root, index, TREE_VALUE);
int k = 3;
System.out.println(kthSmallest(root, k));
}
//递归:中序遍历即为从小到大排序,找到第k个元素即可
/*static List<Integer> res = new ArrayList<>();
public static List<Integer> inorder(TreeNode root) {
if (root != null) {
inorder(root.left);
res.add(root.val);
inorder(root.right);
}
return res;
}
public static int kthSmallest(TreeNode root, int k) {
inorder(root);
return res.get(k - 1);
}*/
//迭代:用stack
public static int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
int num = 0;
int res;
while(curr != null || !stack.isEmpty()){
while (curr != null){
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res = curr.val;
num ++;
curr = curr.right;
if(num == k)
return res;
}
return 0;
}
}