Nested Array Inorder Traversal Generator

Medium
34
36.6% Acceptance

In this lab, you will implement a generator function that performs an inorder traversal of a given multi-dimensional array of integers. The generator object should yield integers in the same order as the inorder traversal.

A multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays.

Inorder traversal iterates over each array from left to right, yielding any integers it encounters or applying inorder traversal to any arrays it encounters.

Examples

Consider the following example:

const arr = [[[6]], [1, 3], []]; const generator = inorderTraversal(arr); generator.next().value; // 6 generator.next().value; // 1 generator.next().value; // 3 generator.next().done; // true

In this case, the input arr = [[[6]], [1, 3], []] will produce the output [6, 1, 3] after performing inorder traversal.

Here is another example:

const arr = []; const generator = inorderTraversal(arr); generator.next().done; // true

In this case, the input arr = [] will produce the output [] because there are no integers in the array, so the generator doesn't yield anything.

Constraints

  • 0 <= arr.flat().length <= 105
  • 0 <= arr.flat()[i] <= 105
  • maxNestingDepth <= 105

Can you solve this without creating a new flattened version of the array?

Challenges

  1. Write a function inorderTraversal that takes a multi-dimensional array as input and returns a generator object that yields integers in the same order as an inorder traversal.
  2. The generator function must not create a new flattened version of the input array.
  3. Make sure to export the function