# General

## 155 Min Stack

Use 2 stacks

```java
//   |1|  
//   |3|      |1|
//   |2|      |2|
//  stack   minStack
public class MinStack{
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
        this.stack = new Stack<Integer>();
        this.minStack = new Stack<Integer>();
    }

    void push(int number) {
        stack.push(number);
        if (minStack.empty()) minStack.push(number);
        else{
            if (number <= minStack.peek()) minStack.push(number);
        }
    }

    int pop() {
        if (stack.peek().equals(minStack.peek())){ // compare using equals()
            minStack.pop();
        }
        return stack.pop();
    }

    int min() {
        return minStack.peek();
    }
```

## Binary Tree node increasing order

1. curQueue 儲存當前node
2. nxtQueue 儲存下一層node

   ```java
   public static List<List<Integer>>binaryTreeDepthOrder(BinaryTreeNode<Integer> tree){
    Queue<BinaryTreeNode<Integer>> curLevel = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();
    curLevel.add(tree);

    while(!curLevel.isEmpty()){
        List<Integer> levelVal = new ArrayList<>();
        Queue<BinaryTreeNode<Integer>> nextLevel = new LinkedList<>();

        while(!curLevel.isEmpty()){
            BinaryTreeNode<Integer> node = curLevel.poll();

            // null check first, a null can still add into a Queue
            if (node != null){
                levelVal.add(node.data);

                nextLevel.add(node.left);
                nextLevel.add(node.right);
            }
        }
        if (!levelVal.isEmpty()) {
            result.add(levelVal);
        }
        curLevel = nextLevel;
    }

    return result;
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://netjimmy.gitbook.io/code-interview-note/stack/general.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
