|
| 1 | +package LeetCodeJava.Stack; |
| 2 | + |
| 3 | +// https://leetcode.com/problems/binary-search-tree-iterator/description/ |
| 4 | + |
| 5 | +import LeetCodeJava.DataStructure.TreeNode; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Stack; |
| 10 | + |
| 11 | +/** |
| 12 | + * 173. Binary Search Tree Iterator |
| 13 | + * Solved |
| 14 | + * Medium |
| 15 | + * Topics |
| 16 | + * Companies |
| 17 | + * Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST): |
| 18 | + * |
| 19 | + * BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST. |
| 20 | + * boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise returns false. |
| 21 | + * int next() Moves the pointer to the right, then returns the number at the pointer. |
| 22 | + * Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the smallest element in the BST. |
| 23 | + * |
| 24 | + * You may assume that next() calls will always be valid. That is, there will be at least a next number in the in-order traversal when next() is called. |
| 25 | + * |
| 26 | + * |
| 27 | + * |
| 28 | + * Example 1: |
| 29 | + * |
| 30 | + * |
| 31 | + * Input |
| 32 | + * ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] |
| 33 | + * [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []] |
| 34 | + * Output |
| 35 | + * [null, 3, 7, true, 9, true, 15, true, 20, false] |
| 36 | + * |
| 37 | + * Explanation |
| 38 | + * BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]); |
| 39 | + * bSTIterator.next(); // return 3 |
| 40 | + * bSTIterator.next(); // return 7 |
| 41 | + * bSTIterator.hasNext(); // return True |
| 42 | + * bSTIterator.next(); // return 9 |
| 43 | + * bSTIterator.hasNext(); // return True |
| 44 | + * bSTIterator.next(); // return 15 |
| 45 | + * bSTIterator.hasNext(); // return True |
| 46 | + * bSTIterator.next(); // return 20 |
| 47 | + * bSTIterator.hasNext(); // return False |
| 48 | + * |
| 49 | + * |
| 50 | + * Constraints: |
| 51 | + * |
| 52 | + * The number of nodes in the tree is in the range [1, 105]. |
| 53 | + * 0 <= Node.val <= 106 |
| 54 | + * At most 105 calls will be made to hasNext, and next. |
| 55 | + * |
| 56 | + * |
| 57 | + * Follow up: |
| 58 | + * |
| 59 | + * Could you implement next() and hasNext() to run in average O(1) time and use O(h) memory, where h is the height of the tree? |
| 60 | + * |
| 61 | + */ |
| 62 | +/** |
| 63 | + * NOTE !!! |
| 64 | + * |
| 65 | + * -> in-order traversal retrieves the keys in ascending sorted order. |
| 66 | + */ |
| 67 | +public class BinarySearchTreeIterator { |
| 68 | + /** |
| 69 | + * Definition for a binary tree node. |
| 70 | + * public class TreeNode { |
| 71 | + * int val; |
| 72 | + * TreeNode left; |
| 73 | + * TreeNode right; |
| 74 | + * TreeNode() {} |
| 75 | + * TreeNode(int val) { this.val = val; } |
| 76 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 77 | + * this.val = val; |
| 78 | + * this.left = left; |
| 79 | + * this.right = right; |
| 80 | + * } |
| 81 | + * } |
| 82 | + */ |
| 83 | + /** |
| 84 | + * Your BSTIterator object will be instantiated and called as such: |
| 85 | + * BSTIterator obj = new BSTIterator(root); |
| 86 | + * int param_1 = obj.next(); |
| 87 | + * boolean param_2 = obj.hasNext(); |
| 88 | + */ |
| 89 | + // V0 |
| 90 | + // IDEA : tree traversal + list + sorting |
| 91 | + class BSTIterator { |
| 92 | + |
| 93 | + // attr |
| 94 | + TreeNode treeNode; |
| 95 | + List<Integer> cache; |
| 96 | + |
| 97 | + public BSTIterator(TreeNode root) { |
| 98 | + this.treeNode = root; |
| 99 | + this.cache = new ArrayList<>(); |
| 100 | + this.getValues(root); |
| 101 | + // ordering (ascending order) |
| 102 | + this.cache.sort(Integer::compareTo); // ??? |
| 103 | + } |
| 104 | + |
| 105 | + public int next() { |
| 106 | + int tmp = this.cache.get(0); |
| 107 | + this.cache.remove(0); |
| 108 | + return tmp; |
| 109 | + } |
| 110 | + |
| 111 | + public boolean hasNext() { |
| 112 | + return !this.cache.isEmpty(); |
| 113 | + } |
| 114 | + |
| 115 | + private void getValues(TreeNode root){ |
| 116 | + if (root == null){ |
| 117 | + return; // ? |
| 118 | + } |
| 119 | + // pre-order traversal (root -> left -> right) |
| 120 | + this.cache.add(root.val); |
| 121 | + |
| 122 | + if (root.left != null){ |
| 123 | + this.getValues(root.left); |
| 124 | + } |
| 125 | + if (root.right != null){ |
| 126 | + this.getValues(root.right); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // V1 |
| 132 | + // IDEA: STACK |
| 133 | + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/52647/nice-comparison-and-short-solution-by-st-jcmg/ |
| 134 | + public class BSTIterator_1 { |
| 135 | + |
| 136 | + private TreeNode visit; |
| 137 | + private Stack<TreeNode> stack; |
| 138 | + |
| 139 | + public BSTIterator_1(TreeNode root) { |
| 140 | + visit = root; |
| 141 | + stack = new Stack(); |
| 142 | + } |
| 143 | + |
| 144 | + public boolean hasNext() { |
| 145 | + return visit != null || !stack.empty(); |
| 146 | + } |
| 147 | + |
| 148 | + public int next() { |
| 149 | + while (visit != null) { |
| 150 | + stack.push(visit); |
| 151 | + visit = visit.left; |
| 152 | + } |
| 153 | + TreeNode next = stack.pop(); |
| 154 | + visit = next.right; |
| 155 | + return next.val; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + // V2 |
| 160 | + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/52525/my-solutions-in-3-languages-with-stack-b-ktax/ |
| 161 | + // IDEA: STACK |
| 162 | + public class BSTIterator_2 { |
| 163 | + private Stack<TreeNode> stack = new Stack<TreeNode>(); |
| 164 | + |
| 165 | + public BSTIterator_2(TreeNode root) { |
| 166 | + pushAll(root); |
| 167 | + } |
| 168 | + |
| 169 | + /** @return whether we have a next smallest number */ |
| 170 | + public boolean hasNext() { |
| 171 | + return !stack.isEmpty(); |
| 172 | + } |
| 173 | + |
| 174 | + /** @return the next smallest number */ |
| 175 | + public int next() { |
| 176 | + TreeNode tmpNode = stack.pop(); |
| 177 | + pushAll(tmpNode.right); |
| 178 | + return tmpNode.val; |
| 179 | + } |
| 180 | + |
| 181 | + private void pushAll(TreeNode node) { |
| 182 | + for (; node != null; stack.push(node), node = node.left); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // V3 |
| 187 | + // https://leetcode.com/problems/binary-search-tree-iterator/solutions/1965120/easy-iterative-using-stack-space-oh-inst-0ov7/ |
| 188 | + // IDEA : STACK |
| 189 | + /** |
| 190 | + * IDEA: |
| 191 | + * |
| 192 | + * 1. Brute force way - You can do in-order traversal and |
| 193 | + * put each element into an ArrayList (additional space). |
| 194 | + * Then use that to check hasNext() or next() element. |
| 195 | + * However, that approach will use extra space of O(n). |
| 196 | + * |
| 197 | + * 2. This approach: |
| 198 | + * a) Here we will use our own Stack (basically do in-order traversal Iteratively, |
| 199 | + * instead of recursively). Reason, being we have more control here and no need to use extra space of O(n) for ArrayList to store all elements first. |
| 200 | + * b) We get space of O(h) only instead of O(n). [h: is height of tree] |
| 201 | + * |
| 202 | + */ |
| 203 | + class BSTIterator_3 { |
| 204 | + Stack<TreeNode> stack; |
| 205 | + public BSTIterator_3(TreeNode root) { |
| 206 | + stack = new Stack<>(); |
| 207 | + TreeNode node = root; |
| 208 | + updateStack(node); // update stack |
| 209 | + } |
| 210 | + public int next() { |
| 211 | + TreeNode toRemove = stack.pop(); |
| 212 | + updateStack(toRemove.right); // before return node, first update stack further |
| 213 | + return toRemove.val; |
| 214 | + } |
| 215 | + public boolean hasNext() { |
| 216 | + return !stack.isEmpty(); |
| 217 | + } |
| 218 | + // ------------------- |
| 219 | + public void updateStack(TreeNode node){ |
| 220 | + while(node != null){ |
| 221 | + stack.add(node); |
| 222 | + node = node.left; |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | +} |
0 commit comments