Implement flattenTree(nodes, childrenKey)
The inverse of buildTree. Write flattenTree(nodes, childrenKey) that takes an array of root nodes (each possibly holding nested children under childrenKey) and returns a single flat array in pre-order (a node before its children, top to bottom).
flattenTree(
[{ id: 1, children: [{ id: 2, children: [{ id: 3, children: [] }] }, { id: 4, children: [] }] }],
"children"
);
// nodes in pre-order: ids [1, 2, 3, 4]