Nested Array Generator

GeneratorsIteratorsArrays

Write a generator function inorderTraversal(arr) that yields every integer in a multidimensional array, in order.

Because it is a generator, the caller can take values one at a time and stop early — nothing is materialised up front.

Examples

Example 1
Input
[...inorderTraversal([1, [2, 3], [[4]]])];
Output
[1, 2, 3, 4]
Explanation
Spreading drains the generator, but next() one value at a time works just as well.

Constraints

  • 0 <= number of integers <= 10^5
  • Nesting may be arbitrarily deep

Notes

  • `yield*` delegates to a recursive call and forwards every value from it.
  • Calling a generator function runs none of its body — the first `next()` does.

Hints

Read the full write-up for Nested Array Generator
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it