File tree 3 files changed +47
-2
lines changed
3 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 152
152
| 170 | 🔒 [ Two Sum III - Data Structure Design] ( https://leetcode.com/problems/two-sum-iii-data-structure-design ) | | |
153
153
| 171 | [ Excel Sheet Column Number] ( https://leetcode.com/problems/excel-sheet-column-number ) | [ ![ Java] ( assets/java.png )] ( src/ExcelSheetColumnNumber.java ) [ ![ Python] ( assets/python.png )] ( python/excel_sheet_column_number.py ) | |
154
154
| 172 | [ Factoring Trailing Zeroes] ( https://leetcode.com/problems/factorial-trailing-zeroes ) | [ ![ Java] ( assets/java.png )] ( src/FactorialTrailingZeros.java ) [ ![ Python] ( assets/python.png )] ( python/factorial_trailing_zeroes.py ) | |
155
- | 173 | [ Binary Search Tree Iterator] ( https://leetcode.com/problems/binary-search-tree-iterator ) | | |
155
+ | 173 | [ Binary Search Tree Iterator] ( https://leetcode.com/problems/binary-search-tree-iterator ) | [ ![ Java ] ( assets/java.png )] ( src/BinarySearchTreeIterator.java ) | |
156
156
| 179 | [ Largest Number] ( https://leetcode.com/problems/largest-number ) | | |
157
157
| 187 | [ Repeated DNA Sequences] ( https://leetcode.com/problems/repeated-dna-sequences ) | | |
158
158
| 189 | [ Rotate Array] ( https://leetcode.com/problems/rotate-array ) | [ ![ Java] ( assets/java.png )] ( src/RotateArray.java ) [ ![ Python] ( assets/python.png )] ( python/rotate_array.py ) | |
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/binary-search-tree-iterator
2
+ // T: O(N)
3
+ // T(next): O(1)
4
+ // T(hasNext): O(1)
5
+ // S: O(log(N))
6
+
7
+ import java .util .Stack ;
8
+
9
+ public class BinarySearchTreeIterator {
10
+
11
+ public static final class BSTIterator {
12
+ private final Stack <TreeNode > stack = new Stack <>();
13
+
14
+ public BSTIterator (TreeNode root ) {
15
+ addLeftNodesPathToStack (root );
16
+ }
17
+
18
+ public int next () {
19
+ final TreeNode node = stack .pop ();
20
+ addLeftNodesPathToStack (node .right );
21
+ return node .val ;
22
+ }
23
+
24
+ public boolean hasNext () {
25
+ return !stack .isEmpty ();
26
+ }
27
+
28
+ private void addLeftNodesPathToStack (TreeNode root ) {
29
+ if (root == null ) return ;
30
+ stack .push (root );
31
+ addLeftNodesPathToStack (root .left );
32
+ }
33
+ }
34
+ }
Original file line number Diff line number Diff line change 1
1
public class HelloWorld {
2
2
public static void main (String [] args ) {
3
- System .out .println (CompareVersionNumbers .compareVersion ("0.1" , "1.1" ));
3
+ TreeNode root = new TreeNode (7 );
4
+ root .left = new TreeNode (3 );
5
+ root .right = new TreeNode (15 );
6
+ root .right .left = new TreeNode (9 );
7
+ root .right .right = new TreeNode (20 );
8
+
9
+ TreePrinter .print (root );
10
+
11
+ BinarySearchTreeIterator .BSTIterator iterator = new BinarySearchTreeIterator .BSTIterator (root );
12
+ while (iterator .hasNext ()) {
13
+ System .out .println (iterator .next ());
14
+ }
4
15
}
5
16
}
You can’t perform that action at this time.
0 commit comments