Back to all solutions
#2649 - Nested Array Generator
Problem Description
Given a multi-dimensional array of integers, return a generator object which yields integers in the same order as 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.
Solution
/**
* @param {Array} input
* @return {Generator}
*/
var inorderTraversal = function* (input) {
for (const item of input) {
if (!Array.isArray(item)) yield item;
else yield* inorderTraversal(item);
}
};