[LeetCode] Binary Search Tree Iterator, problem solving report
Question LeetCode:
mplement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.Credits:Special thanks to @ts for adding this problem and creating all test cases.
In fact, it is not difficult to think about LeetCode. Many questions without thinking can be clearly illustrated at first glance. The same is true for this question. To implement the Iterator of the Binary Search Tree, find the current minimum value each time. Just draw a graph and you will know that this topic is to examine the ordinal traversal of a binary tree. Without considering the space complexity, we can easily write the AC code of the Binary Search Tree:
import java.util.ArrayDeque;import java.util.Stack;public class BSTIterator {private ArrayDeque
mArrayDeque;public BSTIterator(TreeNode root) {mArrayDeque = new ArrayDeque
();bTreeInorderTraverse(root);}private void bTreeInorderTraverse(TreeNode root) {TreeNode p = root;Stack
tmpStack = new Stack
();while (p != null || ! tmpStack.empty()) {if (p != null) {tmpStack.push(p);p = p.left;} else {p = tmpStack.pop();mArrayDeque.add(p);p = p.right;}}}public boolean hasNext() {return ! mArrayDeque.isEmpty();}public int next() {if (hasNext()) {return mArrayDeque.poll().val;} else {return -1;}}public static class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}}
The space complexity in the optimization question is O (h), and the space complexity I use is O (2n), which does not meet the requirements of the question. Therefore, you need to consider how to modify the code logic to solve this problem. The idea is to use the ordinal traversal of a binary tree, but in the case of limited space (ps: only O (h), we can not complete all the ordinal traversal operations in the constructor. The idea is as follows: Apply for a stack of only h size. In the constructor, add all the left children of the root node of the tree to the stack. In the next () function, the top node of the stack is displayed. If the node has a right child, all the left children of the right child and the Right child are added to the stack. The idea is simple. To put it bluntly, it is to break down the traversal algorithm in the middle order and complete it together in the constructor and next method. The AC code is as follows:
import java.util.Stack;public class BSTIterator {private Stack
mStack;public BSTIterator(TreeNode root) {mStack = new Stack
();while (root != null) {mStack.push(root);root = root.left;}}public boolean hasNext() {return ! mStack.empty();}public int next() {TreeNode p = mStack.pop();int res = p.val;if (p.right != null) {TreeNode node = p.right;while (node != null) {mStack.push(node);node = node.left;}}return res;}public static class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}}}