|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.Queue; |
| 7 | +import java.util.Stack; |
| 8 | + |
| 9 | +/** |
| 10 | + * 173. Binary Search Tree Iterator |
| 11 | + * Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. |
| 12 | + * <p> |
| 13 | + * Calling next() will return the next smallest number in the BST. |
| 14 | + * <p> |
| 15 | + * Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. |
| 16 | + */ |
| 17 | +public class _173 { |
| 18 | + |
| 19 | + public static class Solution1 { |
| 20 | + |
| 21 | + public static class BSTIterator { |
| 22 | + |
| 23 | + private Queue<Integer> queue; |
| 24 | + |
| 25 | + /** |
| 26 | + * My natural idea is to use a queue to hold all elements in the BST, traverse it while constructing the iterator, although |
| 27 | + * this guarantees O(1) hasNext() and next() time, but it uses O(n) memory. |
| 28 | + */ |
| 29 | + //Cheers! Made it AC'ed at first shot! Praise the Lord! Practice does make perfect! |
| 30 | + //I created a new class to do it using Stack to meet O(h) memory: {@link fishercoder.algorithms._173_using_stack} |
| 31 | + public BSTIterator(TreeNode root) { |
| 32 | + queue = new LinkedList<Integer>(); |
| 33 | + if (root != null) dfs(root, queue); |
| 34 | + } |
| 35 | + |
| 36 | + private void dfs(TreeNode root, Queue<Integer> q) { |
| 37 | + if (root.left != null) dfs(root.left, q); |
| 38 | + q.offer(root.val); |
| 39 | + if (root.right != null) dfs(root.right, q); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return whether we have a next smallest number |
| 44 | + */ |
| 45 | + public boolean hasNext() { |
| 46 | + return !queue.isEmpty(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return the next smallest number |
| 51 | + */ |
| 52 | + public int next() { |
| 53 | + return queue.poll(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static class Solution2 { |
| 59 | + public static class BSTIterator { |
| 60 | + /** |
| 61 | + * This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we |
| 62 | + * push all its right nodes into the stack if there are any. |
| 63 | + * This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge |
| 64 | + * since h could be much smaller than n. Cheers! |
| 65 | + */ |
| 66 | + |
| 67 | + private Stack<TreeNode> stack; |
| 68 | + |
| 69 | + public BSTIterator(TreeNode root) { |
| 70 | + stack = new Stack(); |
| 71 | + pushToStack(root, stack); |
| 72 | + } |
| 73 | + |
| 74 | + private void pushToStack(TreeNode root, Stack<TreeNode> stack) { |
| 75 | + while (root != null) { |
| 76 | + stack.push(root); |
| 77 | + root = root.left; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public boolean hasNext() { |
| 82 | + return !stack.isEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + public int next() { |
| 86 | + TreeNode curr = stack.pop(); |
| 87 | + pushToStack(curr.right, stack); |
| 88 | + return curr.val; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments