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; }
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); }
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; }
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; }
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(); }
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; } }
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); } }
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(); } }
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(); } }
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(); } }