Skip to content

Commit 5810c77

Browse files
authored
Create Solution.java
1 parent a5c74e0 commit 5810c77

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int kthSmallest(TreeNode root, int k) {
3+
Deque<TreeNode> stack = new ArrayDeque<>();
4+
while (!stack.isEmpty() || root != null) {
5+
while (root != null) {
6+
stack.push(root);
7+
root = root.left;
8+
}
9+
root = stack.pop();
10+
if (--k == 0) {
11+
return root.val;
12+
}
13+
root = root.right;
14+
}
15+
return 0;
16+
}
17+
}

0 commit comments

Comments
 (0)