Skip to content

Commit 9b59fa9

Browse files
committed
Add Solution.java to problems 0230
1 parent 1fc6583 commit 9b59fa9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int kthSmallest(TreeNode root, int k) {
12+
Stack<TreeNode> stack = new Stack();
13+
int res = 0;
14+
while (true) {
15+
while (root != null) {
16+
stack.push(root);
17+
root = root.left;
18+
}
19+
if (k-- == 0 || stack.isEmpty()) break;
20+
TreeNode node = stack.pop();
21+
res = node.val;
22+
root = node.right;
23+
}
24+
return res;
25+
}
26+
}

0 commit comments

Comments
 (0)