Skip to content

Commit

Permalink
update 173 java, cheahtsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Dec 28, 2024
1 parent ca0c835 commit 9016a13
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
66 changes: 66 additions & 0 deletions doc/cheatsheet/bst.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
- LC 230
- LC 538
- LC 1038
- Track BST's `next` `right` node val
- LC 173

```java
// below will print BST elements in ascending ordering
// java
Expand Down Expand Up @@ -559,6 +562,69 @@ class BSTIterator(object):
return self.stack.pop(0) # NOTE here
```

```java
// java
// LC 173
// V1
// IDEA: STACK
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/
public class BSTIterator_1 {

private TreeNode visit;
private Stack<TreeNode> stack;

public BSTIterator_1(TreeNode root) {
visit = root;
stack = new Stack();
}

public boolean hasNext() {
return visit != null || !stack.empty();
}

/**
* NOTE !!! next() method logic
*/
public int next() {
/**
* NOTE !!!
*
* since BST property is as below:
*
* - For each node
* - `left sub node < than current node`
* - `right sub node > than current node`
*
* so, within the loop over `left node`, we keep finding
* the `smaller` node, and find the `relative smallest` node when the while loop ended
* -> so the `relative smallest` node is the final element we put into stack
* -> so it's also the 1st element pop from stack
* -> which is the `next small` node
*/
while (visit != null) {
stack.push(visit);
visit = visit.left;
}
// Stack: LIFO (last in, first out)
/**
* NOTE !!! we pop the `next small` node from stack
*/
TreeNode next = stack.pop();
/**
* NOTE !!! and set the `next small` node's right node as next node
*/
visit = next.right;
/**
* NOTE !!! we return the `next small` node's right node val
* because of the requirement
*
* e.g. : int next() Moves the pointer to the right, then returns the number at the pointer.
*/
return next.val;
}
}
```

### 2-6) Search in a Binary Search Tree
```python
# LC 700 Search in a Binary Search Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ private void getValues(TreeNode root){
// V1
// IDEA: STACK
// https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/
// https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/bst.md
public class BSTIterator_1 {

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

/**
* NOTE !!! next() method logic
*/
public int next() {
while (visit != null) {
/**
* NOTE !!!
*
* since BST property is as below:
*
* - For each node
* - `left sub node < than current node`
* - `right sub node > than current node`
*
* so, within the loop over `left node`, we keep finding
* the `smaller` node, and find the `relative smallest` node when the while loop ended
* -> so the `relative smallest` node is the final element we put into stack
* -> so it's also the 1st element pop from stack
* -> which is the `next small` node
*/
while (visit != null) {
stack.push(visit);
visit = visit.left;
}
// Stack: LIFO (last in, first out)
/**
* NOTE !!! we pop the `next small` node from stack
*/
TreeNode next = stack.pop();
/**
* NOTE !!! and set the `next small` node's right node as next node
*/
visit = next.right;
/**
* NOTE !!! we return the `next small` node's right node val
* because of the requirement
*
* e.g. : int next() Moves the pointer to the right, then returns the number at the pointer.
*/
return next.val;
}
}
Expand Down

0 comments on commit 9016a13

Please sign in to comment.