Skip to content

Commit ca0c835

Browse files
committed
add 173 java
1 parent f13008c commit ca0c835

File tree

5 files changed

+291
-11
lines changed

5 files changed

+291
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
101| [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [Python](./leetcode_python/Stack/symmetric-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/SymmetricTree.java) | _O(n)_ | _O(h)_ | Easy |`good basic`,`bfs`, `dfs`, `linkedin`, `M$`,`amazon`, `fb`, google| AGAIN********** (5)
438438
150| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Python](./leetcode_python/Stack/evaluate-reverse-polish-notation.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/EvaluateReversePolishNotation.java)| _O(n)_| _O(n)_| Medium |stack, good trick,`amazon`| AGAIN********* (5)
439439
155| [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](./leetcode_python/Stack/min-stack.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/MinStack_.java) | _O(n)_ | _O(1)_ | Medium |basic, stack, data structure, `amazon`| OK******* (5)
440-
173| [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)| [Python](./leetcode_python/Stack/binary-search-tree-iterator.py) | _O(1)_| _O(h)_| Medium |`good basic`,`tree, stack, M$`, `linkedin`, `google`,`amazon`,`fb`| OK***** (5)
440+
173| [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/)| [Python](./leetcode_python/Stack/binary-search-tree-iterator.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java)| _O(1)_| _O(h)_| Medium |`good basic`, stack,`tree, stack, M$`, `linkedin`, `google`,`amazon`,`fb`, google| OK***** (5)
441441
224| [Basic Calculator](https://leetcode.com/problems/basic-calculator/) |[Python](./leetcode_python/Stack/basic-calculator.py)| _O(n)_| _O(n)_| Hard |`basic`,`trick`,`stack`, LC 227, amazon| AGAIN******* (3)
442442
227| [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) |[Python](./leetcode_python/Stack/basic-calculator-ii.py)| _O(n)_| _O(n)_| Medium |delay op, LC 224, `good trick`,`stack`,`airbnb` ,`fb`, amazon| AGAIN************** (6) (MUST)
443443
232| [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | [Python](./leetcode_python/Stack/implement-queue-using-stacks.py) | _O(1), amortized_| _O(n)_| Easy | stack, stack-queue, EPI, LintCode, `amazon`| OK** (2)

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241228: 379
1+
20241228: 379,173
22
20241227: 079,212(todo),362,849,855(todo)
33
20241222: 369,311
44
20241221: 370

data/to_review.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
2025-02-21 -> ['379']
1+
2025-02-21 -> ['379,173']
22
2025-02-20 -> ['079,212(todo),362,849,855(todo)']
33
2025-02-15 -> ['369,311']
44
2025-02-14 -> ['370']
55
2025-02-13 -> ['815,871,593,1109']
66
2025-02-07 -> ['560,523']
77
2025-02-01 -> ['304,853,325']
8-
2025-01-31 -> ['379']
8+
2025-01-31 -> ['379,173']
99
2025-01-30 -> ['079,212(todo),362,849,855(todo)']
1010
2025-01-26 -> ['370(todo)']
1111
2025-01-25 -> ['369,311']
1212
2025-01-24 -> ['370', '34,767']
1313
2025-01-23 -> ['815,871,593,1109']
1414
2025-01-20 -> ['722,380']
1515
2025-01-19 -> ['33,81']
16-
2025-01-18 -> ['379']
16+
2025-01-18 -> ['379,173']
1717
2025-01-17 -> ['079,212(todo),362,849,855(todo)', '560,523', '253']
1818
2025-01-16 -> ['776,31']
1919
2025-01-15 -> ['004(todo),34(todo),162(todo),275(todo)']
2020
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2121
2025-01-12 -> ['369,311']
2222
2025-01-11 -> ['370', '304,853,325', '394']
23-
2025-01-10 -> ['379', '815,871,593,1109', '833,950']
23+
2025-01-10 -> ['379,173', '815,871,593,1109', '833,950']
2424
2025-01-09 -> ['079,212(todo),362,849,855(todo)']
25-
2025-01-05 -> ['379', '370(todo)']
25+
2025-01-05 -> ['379,173', '370(todo)']
2626
2025-01-04 -> ['079,212(todo),362,849,855(todo)', '369,311', '560,523', '53,210,207']
2727
2025-01-03 -> ['370', '34,767', '444']
28-
2025-01-02 -> ['379', '815,871,593,1109', '1188,130,855(again)']
28+
2025-01-02 -> ['379,173', '815,871,593,1109', '1188,130,855(again)']
2929
2025-01-01 -> ['079,212(todo),362,849,855(todo)']
30-
2024-12-31 -> ['379']
31-
2024-12-30 -> ['379', '079,212(todo),362,849,855(todo)', '369,311', '722,380']
32-
2024-12-29 -> ['379', '079,212(todo),362,849,855(todo)', '370', '304,853,325', '33,81']
30+
2024-12-31 -> ['379,173']
31+
2024-12-30 -> ['379,173', '079,212(todo),362,849,855(todo)', '369,311', '722,380']
32+
2024-12-29 -> ['379,173', '079,212(todo),362,849,855(todo)', '370', '304,853,325', '33,81']
3333
2024-12-28 -> ['079,212(todo),362,849,855(todo)', '815,871,593,1109', '900']
3434
2024-12-27 -> ['369,311', '560,523', '253', '26,27', '802,1197,26']
3535
2024-12-26 -> ['370', '776,31']
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
package LeetCodeJava.Stack;
2+
3+
// https://leetcode.com/problems/binary-search-tree-iterator/description/
4+
5+
import LeetCodeJava.DataStructure.TreeNode;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.Stack;
10+
11+
/**
12+
* 173. Binary Search Tree Iterator
13+
* Solved
14+
* Medium
15+
* Topics
16+
* Companies
17+
* Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
18+
*
19+
* BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
20+
* boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false.
21+
* int next() Moves the pointer to the right, then returns the number at the pointer.
22+
* Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST.
23+
*
24+
* You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called.
25+
*
26+
*
27+
*
28+
* Example 1:
29+
*
30+
*
31+
* Input
32+
* ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
33+
* [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
34+
* Output
35+
* [null, 3, 7, true, 9, true, 15, true, 20, false]
36+
*
37+
* Explanation
38+
* BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
39+
* bSTIterator.next(); // return 3
40+
* bSTIterator.next(); // return 7
41+
* bSTIterator.hasNext(); // return True
42+
* bSTIterator.next(); // return 9
43+
* bSTIterator.hasNext(); // return True
44+
* bSTIterator.next(); // return 15
45+
* bSTIterator.hasNext(); // return True
46+
* bSTIterator.next(); // return 20
47+
* bSTIterator.hasNext(); // return False
48+
*
49+
*
50+
* Constraints:
51+
*
52+
* The number of nodes in the tree is in the range [1, 105].
53+
* 0 <= Node.val <= 106
54+
* At most 105 calls will be made to hasNext, and next.
55+
*
56+
*
57+
* Follow up:
58+
*
59+
* Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree?
60+
*
61+
*/
62+
/**
63+
* NOTE !!!
64+
*
65+
* -> in-order traversal retrieves the keys in ascending sorted order.
66+
*/
67+
public class BinarySearchTreeIterator {
68+
/**
69+
* Definition for a binary tree node.
70+
* public class TreeNode {
71+
* int val;
72+
* TreeNode left;
73+
* TreeNode right;
74+
* TreeNode() {}
75+
* TreeNode(int val) { this.val = val; }
76+
* TreeNode(int val, TreeNode left, TreeNode right) {
77+
* this.val = val;
78+
* this.left = left;
79+
* this.right = right;
80+
* }
81+
* }
82+
*/
83+
/**
84+
* Your BSTIterator object will be instantiated and called as such:
85+
* BSTIterator obj = new BSTIterator(root);
86+
* int param_1 = obj.next();
87+
* boolean param_2 = obj.hasNext();
88+
*/
89+
// V0
90+
// IDEA : tree traversal + list + sorting
91+
class BSTIterator {
92+
93+
// attr
94+
TreeNode treeNode;
95+
List<Integer> cache;
96+
97+
public BSTIterator(TreeNode root) {
98+
this.treeNode = root;
99+
this.cache = new ArrayList<>();
100+
this.getValues(root);
101+
// ordering (ascending order)
102+
this.cache.sort(Integer::compareTo); // ???
103+
}
104+
105+
public int next() {
106+
int tmp = this.cache.get(0);
107+
this.cache.remove(0);
108+
return tmp;
109+
}
110+
111+
public boolean hasNext() {
112+
return !this.cache.isEmpty();
113+
}
114+
115+
private void getValues(TreeNode root){
116+
if (root == null){
117+
return; // ?
118+
}
119+
// pre-order traversal (root -> left -> right)
120+
this.cache.add(root.val);
121+
122+
if (root.left != null){
123+
this.getValues(root.left);
124+
}
125+
if (root.right != null){
126+
this.getValues(root.right);
127+
}
128+
}
129+
}
130+
131+
// V1
132+
// IDEA: STACK
133+
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/
134+
public class BSTIterator_1 {
135+
136+
private TreeNode visit;
137+
private Stack<TreeNode> stack;
138+
139+
public BSTIterator_1(TreeNode root) {
140+
visit = root;
141+
stack = new Stack();
142+
}
143+
144+
public boolean hasNext() {
145+
return visit != null || !stack.empty();
146+
}
147+
148+
public int next() {
149+
while (visit != null) {
150+
stack.push(visit);
151+
visit = visit.left;
152+
}
153+
TreeNode next = stack.pop();
154+
visit = next.right;
155+
return next.val;
156+
}
157+
}
158+
159+
// V2
160+
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52525/my-solutions-in-3-languages-with-stack-b-ktax/
161+
// IDEA: STACK
162+
public class BSTIterator_2 {
163+
private Stack<TreeNode> stack = new Stack<TreeNode>();
164+
165+
public BSTIterator_2(TreeNode root) {
166+
pushAll(root);
167+
}
168+
169+
/** @return whether we have a next smallest number */
170+
public boolean hasNext() {
171+
return !stack.isEmpty();
172+
}
173+
174+
/** @return the next smallest number */
175+
public int next() {
176+
TreeNode tmpNode = stack.pop();
177+
pushAll(tmpNode.right);
178+
return tmpNode.val;
179+
}
180+
181+
private void pushAll(TreeNode node) {
182+
for (; node != null; stack.push(node), node = node.left);
183+
}
184+
}
185+
186+
// V3
187+
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/1965120/easy-iterative-using-stack-space-oh-inst-0ov7/
188+
// IDEA : STACK
189+
/**
190+
* IDEA:
191+
*
192+
* 1. Brute force way - You can do in-order traversal and
193+
* put each element into an ArrayList (additional space).
194+
* Then use that to check hasNext() or next() element.
195+
* However, that approach will use extra space of O(n).
196+
*
197+
* 2. This approach:
198+
* a) Here we will use our own Stack (basically do in-order traversal Iteratively,
199+
* instead of recursively). Reason, being we have more control here and no need to use extra space of O(n) for ArrayList to store all elements first.
200+
* b) We get space of O(h) only instead of O(n). [h: is height of tree]
201+
*
202+
*/
203+
class BSTIterator_3 {
204+
Stack<TreeNode> stack;
205+
public BSTIterator_3(TreeNode root) {
206+
stack = new Stack<>();
207+
TreeNode node = root;
208+
updateStack(node); // update stack
209+
}
210+
public int next() {
211+
TreeNode toRemove = stack.pop();
212+
updateStack(toRemove.right); // before return node, first update stack further
213+
return toRemove.val;
214+
}
215+
public boolean hasNext() {
216+
return !stack.isEmpty();
217+
}
218+
// -------------------
219+
public void updateStack(TreeNode node){
220+
while(node != null){
221+
stack.add(node);
222+
node = node.left;
223+
}
224+
}
225+
}
226+
227+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
import LeetCodeJava.DataStructure.ListNode;
5+
import LeetCodeJava.DataStructure.TreeNode;
6+
57
import java.util.*;
68

79
public class workspace6 {
@@ -854,4 +856,55 @@ public void release(int number) {
854856
}
855857
}
856858

859+
// LC 173
860+
// https://leetcode.com/problems/binary-search-tree-iterator/
861+
// 4.58 - 5.20 pm
862+
/**
863+
* In a binary search tree ordered such that in each node the
864+
* key is greater than all keys in its left subtree and
865+
* less than all keys in its right subtree,
866+
*
867+
* -> in-order traversal retrieves the keys in ascending sorted order.
868+
*
869+
*/
870+
class BSTIterator {
871+
872+
// attr
873+
TreeNode treeNode;
874+
List<Integer> cache;
875+
876+
public BSTIterator(TreeNode root) {
877+
this.treeNode = root;
878+
this.cache = new ArrayList<>();
879+
this.getValues(root);
880+
// ordering (ascending order)
881+
this.cache.sort(Integer::compareTo); // ???
882+
}
883+
884+
public int next() {
885+
int tmp = this.cache.get(0);
886+
this.cache.remove(0);
887+
return tmp;
888+
}
889+
890+
public boolean hasNext() {
891+
return !this.cache.isEmpty();
892+
}
893+
894+
private void getValues(TreeNode root){
895+
if (root == null){
896+
return; // ?
897+
}
898+
// pre-order traversal (root -> left -> right)
899+
this.cache.add(root.val);
900+
901+
if (root.left != null){
902+
this.getValues(root.left);
903+
}
904+
if (root.right != null){
905+
this.getValues(root.right);
906+
}
907+
}
908+
}
909+
857910
}

0 commit comments

Comments
 (0)