Skip to content

Commit df95d0f

Browse files
committed
KthSmallestElementInABST230
1 parent 1bd2fbc commit df95d0f

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232

3333

3434

35-
# Total: 134
35+
# Total: 135
3636

3737
| Easy | Medium | Hard |
3838
|:----:|:------:|:----:|
39-
| 23 | 84 | 27 |
39+
| 23 | 85 | 27 |
4040

4141

4242
| Question | Solution | Difficulty |
@@ -132,6 +132,7 @@
132132
| [221. Maximal Square](https://leetcode.com/problems/maximal-square/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaximalSquare221.java) | Medium |
133133
| [224. Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BasicCalculator224.java) | Hard |
134134
| [226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/InvertBinaryTree226.java) | Easy |
135+
| [230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/KthSmallestElementInABST230.java) | Medium |
135136
| [233. Number of Digit One](https://leetcode.com/problems/number-of-digit-one/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NumberOfDigitOne233.java) | Hard |
136137
| [240. Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SearchA2DMatrixII240.java) | Medium |
137138
| [241. Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/DifferentWaysToAddParentheses241.java) | Medium |
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Given a binary search tree, write a function kthSmallest to find the kth
3+
* smallest element in it.
4+
*
5+
* Note:
6+
* You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
7+
*
8+
* Follow up:
9+
* What if the BST is modified (insert/delete operations) often and you need to
10+
* find the kth smallest frequently? How would you optimize the kthSmallest
11+
* routine?
12+
*
13+
*/
14+
15+
/**
16+
* Definition for a binary tree node.
17+
* public class TreeNode {
18+
* int val;
19+
* TreeNode left;
20+
* TreeNode right;
21+
* TreeNode(int x) { val = x; }
22+
* }
23+
*/
24+
25+
26+
public class KthSmallestElementInABST230 {
27+
public int kthSmallest(TreeNode root, int k) {
28+
Stack<Integer> st = new Stack<>();
29+
kthSmallest(root, k, st);
30+
return st.pop();
31+
}
32+
33+
public void kthSmallest(TreeNode root, int k, Stack<Integer> st) {
34+
if (root == null) return;
35+
36+
kthSmallest(root.left, k, st);
37+
if (st.size() == k) return;
38+
39+
st.push(root.val);
40+
if (st.size() == k) return;
41+
42+
kthSmallest(root.right, k, st);
43+
}
44+
45+
46+
public int kthSmallest2(TreeNode root, int k) {
47+
int[] i = new int[]{0, 0};
48+
kthSmallest(root, k, i);
49+
return i[1];
50+
}
51+
52+
public void kthSmallest(TreeNode root, int k, int[] i) {
53+
if (root.left != null) kthSmallest(root.left, k, i);
54+
if (i[0] == k) return ;
55+
56+
i[0] = i[0] + 1;
57+
i[1] = root.val;
58+
if (i[0] == k) return;
59+
60+
if (root.right != null) kthSmallest(root.right, k, i);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)