N-ary Tree Preorder Traversal — Advanced Trees
List folder names in the order a backup copies them: each folder before those inside it, finishing each subfolder fully before the next.
Advanced Trees · Easy
The problem
A folder can hold any number of subfolders, and a backup tool copies each folder before the ones inside it. Each node of a tree may have any number of children. Visit a node first, then each of its children subtrees in order from left to right, and report the values in that visiting order.
The story
A backup tool copies a folder tree where each folder can hold any number of subfolders, up to 12 folders in all. It copies each folder before the ones inside it, and finishes one subfolder completely before moving on to the next, left to right. List the folders in the order they're copied; an empty tree gives an empty list.
What to return
- Return the values in preorder (node, then its children from left to right, each child fully before the next).
- An empty tree gives an empty list.
- The tree is given as one flat list: the root value first, then, for every node in level order, a null followed by the values of that node's children (a node with no children adds only its null). Trailing nulls are dropped; [] is an empty tree.
Constraints
- 0 <= number of nodes <= 12
- 0 <= node value <= 20 (values may repeat)
Example
- Input
root = [3,null,6,2,9,null,1,7]- Output
[3, 6, 1, 7, 2, 9]