Day 2

zhanglei 2022年07月23日 379次浏览

Day 2

剑指offer07.重建二叉树

二叉树的遍历知识

image-20220723135419438

题目描述

image-20220723154926422

解答


剑指offer09.两个栈实现队列

题目描述

image-20220723154147194

解答

class CQueue {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public CQueue() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }
    
    public void appendTail(int value) {
        stack1.push(value);
    }
    
    public int deleteHead() {
        // 一定要先判断stack2是否为空!
        if(!stack2.isEmpty()){
            return stack2.pop();
        }else{
            while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
            }
        }
       
        return stack2.isEmpty()?-1:stack2.pop();
    }
}

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue obj = new CQueue();
 * obj.appendTail(value);
 * int param_2 = obj.deleteHead();
 */