Medium · Streams & I/O

Binary tree in-order iterator

Given the root of a binary tree, build an iterator whose next() returns the tree's values in in-order (left subtree, node, right subtree), one per call, and whose hasNext() reports whether any value remains.

Examples

Example 1

[1,2,3,4,5,6,7]

Output: [4, 2, 5, 1, 6, 3, 7]

Example 2

[4,2,6,1,3,5,7]

Output: [1, 2, 3, 4, 5, 6, 7]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Streams & I/O problems