You are given a list of top-level posts, where each post may contain a list of nested comments, and each comment may itself contain more nested replies.
Write a function that flattens this hierarchical discussion structure into a single array in depth-first order.
Each node has the form:
-
id
: unique identifier
-
content
: text
-
children
: array of child comments
Example input:
A valid flattened output in DFS order would be:
[Post A, Comment A1, Reply A1a, Comment A2, Post B]
Follow-up 1: Modify the function so it only flattens nodes up to a given maximum depth. For example, if maxDepth = 1, include the root posts and their direct children, but do not include deeper replies.
Follow-up 2: Implement the same flattening logic iteratively using a loop instead of recursion.