Binary search tree iterator

1. Demand

  • Implement a binary search tree iterator. You will initialize the iterator with the root node of the binary search tree;

  • The call  next() will return the next smallest number in the binary search tree.

     

BSTIterator iterator = new BSTIterator(root);
iterator.next(); // returns 3
iterator.next(); // returns 7
iterator.hasNext(); // returns true
iterator.next(); // returns 9
iterator .hasNext(); // return true
iterator.next(); // return 15
iterator.hasNext(); // return true
iterator.next(); // return 20
iterator.hasNext(); // return false

prompt:

The time complexity of next() and hasNext() operations is O(1) and uses O(h) memory, where h is the height of the tree.
You can assume that the next() call is always valid, that is, when next() is called, there is at least one next smallest number in the BST (think the next smallest number as a whole).

Two, flat binary tree

2.1 Thinking analysis

  1. The so-called flattened binary tree is to straighten the binary tree. We store the middle-order traversal of the BST into the collection, so that the elements in the collection are sorted in ascending order, pointing to the "next smallest element" through an index;
  2. Note that the amortized time complexity of this method is O(1), and the space complexity is O(N), which does not meet the requirements of the problem O(h) memory;

2.2 Code implementation

class BSTIterator {
    List<Integer> res;
    int index;
    public BSTIterator(TreeNode root) {
        res = new ArrayList<>();
        index = 0;
        infixOrder(root);
    }
    
    public int next() {
        int tmp = res.get(index);
        index++;
        return tmp;
    }
    
    public boolean hasNext() {
        if(index < res.size()) {
            return true;
        } else {
            return false;
        }
    }

    public void infixOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        infixOrder(root.left);
        res.add(root.val);
        infixOrder(root.right);
    }
}

2.3 Complexity analysis

  • The amortized time complexity is O(1), the total number of operations to execute the construction method, N times of next(), and N times hasNext() is 3N, and the average number of operations is 3, so the amortized time complexity is O(1);
  • The space complexity is O(N), and the in-order traversal of the collective storage BST consumes O(N) extra space;

Three, iterative method

3.1 Thinking analysis

  1. Here we use the iterative method to obtain the idea of ​​in-order traversal, which is processed in the iterative process, instead of processing after the end of the recursion like method 2;
  2. Use the stack structure to implement and define a method whose function is to push the current node and its left subtree into the stack;

3.2 Code implementation

class BSTIterator {
    Deque<TreeNode> stack;
    public BSTIterator(TreeNode root) {
        stack = new ArrayDeque<>();
        addLeftTree(root);
    }
    
    public int next() {
        TreeNode node = stack.pop();
        if(node.right != null) {
            addLeftTree(node.right);
        }
        return node.val;
    }
    
    public boolean hasNext() {
        return stack.size() > 0;
    }

    private void addLeftTree(TreeNode node) {
        while(node != null) {
            stack.push(node);
            node = node.left;
        }
    }
}

3.3 Complexity analysis

  • The amortized time complexity is O(1);
  • The space complexity is O(log_2N), the space consumed is equivalent to the depth h of the tree;

Four, study address

Author: LeetCode

Link: https://leetcode-cn.com/problems/binary-search-tree-iterator/solution/er-cha-sou-suo-shu-die-dai-qi-by-leetcode/

Guess you like

Origin blog.csdn.net/Sruggle/article/details/114875107