# 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;
    ```

