(LC)173. Binary Search Tree Iterator

173. Binary Search Tree Iterator

Link to the original question: Binary search tree iterator .
Implement a binary search tree iterator class BSTIterator, which represents an iterator that traverses the binary search tree (BST) in order:
BSTIterator(TreeNode root) initializes an object of the BSTIterator class . The root node root of the BST will be given as part of the constructor. The pointer should be initialized to a number that does not exist in the BST, and the number is less than any element in the BST.
boolean hasNext() Returns true if there are numbers traversing to the right of the pointer; otherwise, returns false.
int next() moves the pointer to the right, and then returns the number at the pointer.
Note that the pointer is initialized to a number that does not exist in the BST, so the first call to next() will return the smallest element in the BST.

You can assume that the next() call is always valid, that is, when next() is called, there is at least one next number in the in-order traversal of the BST.

Example:

输入
[“BSTIterator”, “next”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”, “next”, “hasNext”]
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
输出
[null, 3, 7, true, 9, true, 15, true, 20, false]

Interpret
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
bSTIterator.next(); // return 3
bSTIterator.next(); // return 7
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 9
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 15
bSTIterator.hasNext(); // return True
bSTIterator.next(); // return 20
bSTIterator.hasNext(); // return False

prompt:

The number of nodes in the tree is within the range [1, 105]
0 <= Node.val <= 106
Call hasNext and next operations up to 105 times

Advanced:

Can you design a solution that meets the following conditions? The next() and hasNext() operations have an average time complexity of O(1) and use O(h) memory. Where h is the height of the tree.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class BSTIterator {
    
    

    private LinkedList<TreeNode> stack = new LinkedList<>();
    public BSTIterator(TreeNode root) {
    
    
        TreeNode cur = root;
        pushToStack(cur);
    }
    
    public int next() {
    
    
        TreeNode node = stack.pop();
        TreeNode cur = node;
        if (cur.right != null) {
    
    
            cur = cur.right;
            pushToStack(cur);
        }
        return node.val;
    }
    
    public boolean hasNext() {
    
    
        return !stack.isEmpty();
    }
    public void pushToStack(TreeNode treeNode) {
    
    
        TreeNode cur = treeNode;
        while (cur != null) {
    
    
            stack.push(cur);
            if (cur.left != null) {
    
    
                cur = cur.left;
            } else {
    
    
                break;
            }
        }
    }
}

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator obj = new BSTIterator(root);
 * int param_1 = obj.next();
 * boolean param_2 = obj.hasNext();
 */

Guess you like

Origin blog.csdn.net/weixin_45567738/article/details/115276899