722. Flatten a Nested Array Recursively

EasyRecursionRecursion

Given a nested list of integers (a JSON-style array that may contain integers or other nested arrays to arbitrary depth), return a single flat array containing all integers in the order they appear when the input is traversed left-to-right. Implement the flattening recursively.

Input: A JSON-style nested array of integers, e.g. [1,[2,[3,4]],5].

Output: Return a flat array as [v1,v2,...] in left-to-right order.

Examples

Example 1
Input: [1,[2,[3,4]],5]
Output: [1,2,3,4,5]
Explanation: Traverse depth-first: 1, then the nested [2,[3,4]] -> 2,3,4, then 5.
Example 2
Input: [[1,2],[3,4]]
Output: [1,2,3,4]
Explanation: Two sub-arrays concatenated.
Example 3
Input: [[[[1]]]]
Output: [1]
Explanation: Deeply nested single element.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →