Skip to content

2649. Nested Array Generator 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
type MultidimensionalArray = (MultidimensionalArray | number)[];

function* inorderTraversal(
  arr: MultidimensionalArray
): Generator<number, void, unknown> {
  for (const item of arr) {
    if (typeof item === 'number') {
      yield item;
    } else {
      yield* inorderTraversal(item);
    }
  }
}