java.lang.Object
g0101_0200.s0173_binary_search_tree_iterator.BSTIterator

public class BSTIterator extends Object
173 - Binary Search Tree Iterator.<p>Medium</p> <p>Implement the <code>BSTIterator</code> class that represents an iterator over the <strong><a href="https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR)" target="_top">in-order traversal</a></strong> of a binary search tree (BST):</p> <ul> <li><code>BSTIterator(TreeNode root)</code> Initializes an object of the <code>BSTIterator</code> class. The <code>root</code> of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.</li> <li><code>boolean hasNext()</code> Returns <code>true</code> if there exists a number in the traversal to the right of the pointer, otherwise returns <code>false</code>.</li> <li><code>int next()</code> Moves the pointer to the right, then returns the number at the pointer.</li> </ul> <p>Notice that by initializing the pointer to a non-existent smallest number, the first call to <code>next()</code> will return the smallest element in the BST.</p> <p>You may assume that <code>next()</code> calls will always be valid. That is, there will be at least a next number in the in-order traversal when <code>next()</code> is called.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png" alt="" /></p> <p><strong>Input</strong> [&ldquo;BSTIterator&rdquo;, &ldquo;next&rdquo;, &ldquo;next&rdquo;, &ldquo;hasNext&rdquo;, &ldquo;next&rdquo;, &ldquo;hasNext&rdquo;, &ldquo;next&rdquo;, &ldquo;hasNext&rdquo;, &ldquo;next&rdquo;, &ldquo;hasNext&rdquo;] [<a href="7,-3,-15,-null,-null,-9,-20">7, 3, 15, null, null, 9, 20</a>, [], [], [], [], [], [], [], [], []]</p> <p><strong>Output:</strong> [null, 3, 7, true, 9, true, 15, true, 20, false]</p> <p><strong>Explanation:</strong></p> <pre><code> 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 </code></pre> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li> <li><code>0 <= Node.val <= 10<sup>6</sup></code></li> <li>At most <code>10<sup>5</sup></code> calls will be made to <code>hasNext</code>, and <code>next</code>.</li> </ul> <p><strong>Follow up:</strong></p> <ul> <li>Could you implement <code>next()</code> and <code>hasNext()</code> to run in average <code>O(1)</code> time and use <code>O(h)</code> memory, where <code>h</code> is the height of the tree?</li> </ul>
  • Constructor Details

    • BSTIterator

      public BSTIterator(TreeNode root)
  • Method Details

    • next

      public int next()
    • hasNext

      public boolean hasNext()