Skip to content

Commit 9016a13

Browse files
committed
update 173 java, cheahtsheet
1 parent ca0c835 commit 9016a13

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

doc/cheatsheet/bst.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
- LC 230
1515
- LC 538
1616
- LC 1038
17+
- Track BST's `next` `right` node val
18+
- LC 173
19+
1720
```java
1821
// below will print BST elements in ascending ordering
1922
// java
@@ -559,6 +562,69 @@ class BSTIterator(object):
559562
return self.stack.pop(0) # NOTE here
560563
```
561564

565+
```java
566+
// java
567+
// LC 173
568+
// V1
569+
// IDEA: STACK
570+
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/
571+
public class BSTIterator_1 {
572+
573+
private TreeNode visit;
574+
private Stack<TreeNode> stack;
575+
576+
public BSTIterator_1(TreeNode root) {
577+
visit = root;
578+
stack = new Stack();
579+
}
580+
581+
public boolean hasNext() {
582+
return visit != null || !stack.empty();
583+
}
584+
585+
/**
586+
* NOTE !!! next() method logic
587+
*/
588+
public int next() {
589+
/**
590+
* NOTE !!!
591+
*
592+
* since BST property is as below:
593+
*
594+
* - For each node
595+
* - `left sub node < than current node`
596+
* - `right sub node > than current node`
597+
*
598+
* so, within the loop over `left node`, we keep finding
599+
* the `smaller` node, and find the `relative smallest` node when the while loop ended
600+
* -> so the `relative smallest` node is the final element we put into stack
601+
* -> so it's also the 1st element pop from stack
602+
* -> which is the `next small` node
603+
*/
604+
while (visit != null) {
605+
stack.push(visit);
606+
visit = visit.left;
607+
}
608+
// Stack: LIFO (last in, first out)
609+
/**
610+
* NOTE !!! we pop the `next small` node from stack
611+
*/
612+
TreeNode next = stack.pop();
613+
/**
614+
* NOTE !!! and set the `next small` node's right node as next node
615+
*/
616+
visit = next.right;
617+
/**
618+
* NOTE !!! we return the `next small` node's right node val
619+
* because of the requirement
620+
*
621+
* e.g. : int next() Moves the pointer to the right, then returns the number at the pointer.
622+
*/
623+
return next.val;
624+
}
625+
}
626+
```
627+
562628
### 2-6) Search in a Binary Search Tree
563629
```python
564630
# LC 700 Search in a Binary Search Tree

leetcode_java/src/main/java/LeetCodeJava/Stack/BinarySearchTreeIterator.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ private void getValues(TreeNode root){
131131
// V1
132132
// IDEA: STACK
133133
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/
134+
// https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/bst.md
134135
public class BSTIterator_1 {
135136

136137
private TreeNode visit;
@@ -145,13 +146,44 @@ public boolean hasNext() {
145146
return visit != null || !stack.empty();
146147
}
147148

149+
/**
150+
* NOTE !!! next() method logic
151+
*/
148152
public int next() {
149-
while (visit != null) {
153+
/**
154+
* NOTE !!!
155+
*
156+
* since BST property is as below:
157+
*
158+
* - For each node
159+
* - `left sub node < than current node`
160+
* - `right sub node > than current node`
161+
*
162+
* so, within the loop over `left node`, we keep finding
163+
* the `smaller` node, and find the `relative smallest` node when the while loop ended
164+
* -> so the `relative smallest` node is the final element we put into stack
165+
* -> so it's also the 1st element pop from stack
166+
* -> which is the `next small` node
167+
*/
168+
while (visit != null) {
150169
stack.push(visit);
151170
visit = visit.left;
152171
}
172+
// Stack: LIFO (last in, first out)
173+
/**
174+
* NOTE !!! we pop the `next small` node from stack
175+
*/
153176
TreeNode next = stack.pop();
177+
/**
178+
* NOTE !!! and set the `next small` node's right node as next node
179+
*/
154180
visit = next.right;
181+
/**
182+
* NOTE !!! we return the `next small` node's right node val
183+
* because of the requirement
184+
*
185+
* e.g. : int next() Moves the pointer to the right, then returns the number at the pointer.
186+
*/
155187
return next.val;
156188
}
157189
}

0 commit comments

Comments
 (0)