stack

5月12日

1  94 Binary Tree Inorder Traverse  栈

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty())
        {
            while (cur != null)
            {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            list.add(cur.val);
            cur = cur.right;
        }
        return list;
    }
View Code

2 103 Binary Tree Zigzag Level order Treverse

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        traver(root, res, 0);
        return res;
    }
    public void traver(TreeNode root, List<List<Integer>> res, int level)
    {
        if (root == null) return;
        if (res.size() <= level)
        {
            res.add(new LinkedList<>());
        }
        List<Integer> list = res.get(level);
        if (level % 2 == 0) list.add(root.val);
        else list.add(0, root.val);
        traver(root.left, res, level + 1);
        traver(root.right, res, level + 1);
    }
View Code

3 144 Binary Tree Preorder Traverse

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while (node != null || !stack.isEmpty())
        {
            while (node != null)
            {
                res.add(node.val);
                stack.push(node);
                node = node.left;
            }
            if (!stack.isEmpty())
            {
                node = stack.pop();
                node = node.right;
            }
        }
        return res;
    }
View Code

4 145 Binary Tree Postorder Traverse

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (root != null || !stack.isEmpty())
        {
            while (root != null)
            {
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty())
            {
                TreeNode peek = stack.peek();
                if (peek.right != null && pre != peek.right)
                {
                    root = peek.right;
                }
                else
                {
                    stack.pop();
                    res.add(peek.val);
                    pre = peek;
                }
            }
        }
        return res;
    }
View Code

 5 150 Evaluate Reverse Polish Notation

    public int evalRPN(String[] tokens) {
        int a,b;
        Stack<Integer> stack = new Stack<>();
        for (String c : tokens)
        {
            if (c.equals("+"))
            {
                stack.add(stack.pop() + stack.pop());
            }
            else if (c.equals("-"))
            {
                b = stack.pop();
                a = stack.pop();
                stack.add(a - b);
            }else if (c.equals("*"))
            {
                stack.add(stack.pop() * stack.pop());
            }else if (c.equals("/"))
            {
                b = stack.pop();
                a = stack.pop();
                stack.add(a / b);
            }else
            {
                stack.add(Integer.parseInt(c));
            }
        }
        return stack.pop();
    }
View Code

6 155 Min Stack   维护栈和最小值

public class MinStack {
    
    int min = Integer.MAX_VALUE;
    Stack<Integer> stack = new Stack<>();
    /** initialize your data structure here. */
    public MinStack() {
    }
    
    public void push(int x) {
        if (x <= min)
        {
            stack.push(min);
            min = x;
        }
        stack.push(x);
    }
    
    public void pop() {
        if (min == stack.pop()) min = stack.pop();
    }
    
    public int top() {
        return stack.peek();
    }
    
    public int getMin() {
        return min;
    }
}
View Code

7 173 binary Serch Tree Iterator  当next 变量

public class BSTIterator {
    
    Stack<TreeNode> stack = new Stack<>();
    public BSTIterator(TreeNode root) {
        pushAll(root);
    }

    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return !stack.isEmpty();
    }

    /** @return the next smallest number */
    public int next() {
        TreeNode temp = stack.pop();
        pushAll(temp.right);
        return temp.val;
    }
    private void pushAll(TreeNode node)
    {
        for (; node != null; stack.push(node), node = node.left);
    }
}
View Code

8 225  Implement Stack using  Queue push 时把数放后面

public class MyStack {

    Queue<Integer> queue;
    /** Initialize your data structure here. */
    public MyStack() {
        this.queue = new LinkedList<Integer>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue.add(x);
        for (int i = 0; i < queue.size() - 1; i++)
        {
            queue.add(queue.poll());
        }
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }
    
    /** Get the top element. */
    public int top() {
        return queue.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}
View Code

 9 232 Implement Queue using stack 两个staCK

public class MyQueue {

    Stack<Integer> input = new Stack<>();
    Stack<Integer> output = new Stack<>();
    /** Initialize your data structure here. */
    public MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        input.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        peek();
        return output.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if (output.isEmpty())
        {
            while (!input.isEmpty())
            {
                output.push(input.pop());
            }
        }
        return output.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return input.isEmpty() && output.isEmpty();
    }
}
View Code

 

public class MyQueue {

    Stack<Integer> input = new Stack<>();
    Stack<Integer> output = new Stack<>();
    /** Initialize your data structure here. */
    public MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        input.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        peek();
        return output.pop();
    }
    
    /** Get the front element. */
    public int peek() {
        if (output.isEmpty())
        {
            while (!input.isEmpty())
            {
                output.push(input.pop());
            }
        }
        return output.peek();
    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        return input.isEmpty() && output.isEmpty();
    }
}
View Code

 

转载于:https://www.cnblogs.com/whesuanfa/p/6845548.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值