Binary Search Tree Iterator

Given the root of a binary search tree, return the sequence of values produced by an inorder iterator over the tree. Pattern focus: BST Iterator Thinking. The iterator exposes the BST in sorted order, so the output is the same as a full inorder traversal.

Input Format

root = binary tree root

Output Format

values returned by inorder iteration in order

Constraints

  • 0 <= number of nodes <= 10^5
  • -10^9 <= node values <= 10^9
  • root must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

root = [7,3,15,null,null,9,20]

Output:

[3,7,9,15,20]

Explanation:

The inorder iterator visits the BST in sorted order.

Example 2:

Input:

root = [2,1,3]

Output:

[1,2,3]

Explanation:

The iterator returns values from smallest to largest.

Loading...
Binary Search Tree Iterator - Binary Search Tree