Question

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.

Thinking:

  • Method:
    • 使用LinkedList装载中序遍历的结果。O(N) extra space
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    private LinkedList<Integer> list;
    private ListIterator<Integer> cur = null;
    public BSTIterator(TreeNode root) {
        list = new LinkedList<>();
        inorder(root, list);
        cur = list.listIterator(0);
    }
    private void inorder(TreeNode node, List<Integer> result){
        if(node == null) return;
        inorder(node.left, result);
        result.add(node.val);
        inorder(node.right, result);
    }
    /** @return whether we have a next smallest number */
    public boolean hasNext() {
        return cur.hasNext();
    }

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

/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = new BSTIterator(root);
 * while (i.hasNext()) v[f()] = i.next();
 */
  • Method 2: 通过栈实现
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class BSTIterator {
    Stack<TreeNode> stack;
    public BSTIterator(TreeNode root) {
        stack = new Stack<>();
        while(root != null){
            stack.push(root);
            root = root.left;
        }
    }

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

    /** @return the next smallest number */
    public int next() {
        TreeNode cur = stack.pop();
        if(cur.right != null){
            TreeNode next = cur.right;
            while(next != null){
                stack.push(next);
                next = next.left;
            }
        }
        return cur.val;
    }
}

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

二刷

  • 通过链表解决该问题。 ```Java /**
  • Definition for binary tree
  • public class TreeNode {
  • int val;
  • TreeNode left;
  • TreeNode right;
  • TreeNode(int x) { val = x; }
  • } */ public class BSTIterator { private List inorder = new LinkedList<>(); private ListIterator cur = null; public BSTIterator(TreeNode root) { inorder(root, inorder); cur = inorder.listIterator(0); } private void inorder(TreeNode root, List list){ if(root == null) return; inorder(root.left, list); list.add(root.val); inorder(root.right, list); } /** @return whether we have a next smallest number */ public boolean hasNext() { return cur.hasNext(); } /** @return the next smallest number */ public int next() { return cur.next(); } } /**
  • Your BSTIterator will be called like this:
  • BSTIterator i = new BSTIterator(root);
  • while (i.hasNext()) v[f()] = i.next(); */ ```