Question

Formatted question description: https://leetcode.ca/all/173.html

 173	Binary Search Tree Iterator

 Implement 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.

 @tag-tree

Algorithm

The non-recursive form of the in-order traversal of the binary tree requires an additional definition of a stack to assist. The building rule of the binary search tree is left<root<right, and the in-order traversal can extract all nodes from small to large.

Code

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

public class Binary_Search_Tree_Iterator {

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class BSTIterator {

        Stack<TreeNode> sk;

        // @note:@memorize: important feature, min is always going left branch to leaf
        public BSTIterator(TreeNode root) {
            sk = new Stack<>();

            // all the way to leftmost leaf
            while (root != null) {
                sk.push(root);
                root = root.left;
            }

        }

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

        /** @return the next smallest number */
        public int next() {
            TreeNode minNode = sk.pop();

            TreeNode current = minNode;
            // update stack, possible next time min is frmo its right-then-left branch
            if (current.right != null) {
                current = current.right;

                // same logic as in constructor
                while (current != null) {
                    sk.push(current.left);
                    current = current.left;
                }
            }

            return minNode.val;
        }
    }



    public class BSTIteratorrrrrr {

        List<Integer> sorted;

        public BSTIteratorrrrrr(TreeNode root) {
            sorted = new ArrayList<>();
            dfs(root);
            Collections.sort(sorted); // NlogN
        }

        private void dfs(TreeNode root) {

            if (root == null) {
                return;
            }

            sorted.add(root.val);
            dfs(root.left);
            dfs(root.right);
        }

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

        /** @return the next smallest number */
        public int next() {
            return sorted.remove(0);
        }
    }

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */
}

All Problems

All Solutions