-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathKth Smallest Element in a BST.java
executable file
·131 lines (106 loc) · 3.18 KB
/
Kth Smallest Element in a BST.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
M
1527998417
tags: BST, Tree, DFS, Stack
#### Iterative + stack: inorder traversal
- 很容想到Inorder-binary-search-tree Traversal
- Iterative 稍微难想点:先把最左边的add, pop() stack, 加上右边(如果存在); 下一个轮回,如果又左孩子,又是一顿加。
#### Recursive + DFS
- 然后稍微优化一下,确保rst.size() == k 时候,就可以return了
- check leaf => dfs left => add root => dfs right
```
/*
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Hint:
Try to utilize the property of a BST.
What if you could modify the BST node's structure?
The optimal runtime complexity is O(height of BST).
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Hide Company Tags Google
Hide Tags Tree Binary Search
Hide Similar Problems (M) Binary Tree Inorder Traversal
*/
/*
Based on binary seach tree, just do a in-order-traversal.
Store in rst.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
//Iterative
Add all left.
pop top (which will be left-most node)
set node = node.right;
if right != null, add to stack. Will trigger the left-adding-while-loop
if right == null, now node = null. Will not trigger the left-adding-whilte-loop
*/
public class Solution {
public int kthSmallest(TreeNode root, int k) {
if (root == null || k <= 0) {
return -1;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
TreeNode node = root;
while(!stack.isEmpty()) {
//Left first
while (node != null && node.left != null) {
stack.add(node.left);
node = node.left;
}
//Process left/curr
node = stack.pop();
k--;
if (k == 0) {
return node.val;
}
node = node.right;
if (node != null) {
stack.push(node);
}
}
return -1;
}
}
// Recursive
public class Solution {
public int kthSmallest(TreeNode root, int k) {
if (root == null || k <= 0) {
return -1;
}
ArrayList<TreeNode> rst = new ArrayList<TreeNode>();
helper(rst, root, k);
if (rst.size() < k) {
return -1;
}
return rst.get(k - 1).val;
}
public void helper(ArrayList<TreeNode> rst, TreeNode node, int k) {
if (rst.size() == k) {
return;
}
if (node.left == null && node.right == null) {
rst.add(node);
return;
}
if (node.left != null) {
helper(rst, node.left, k);
}
rst.add(node);
if (node.right != null) {
helper(rst, node.right, k);
}
}
}
```