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

Constraints

Example

Input
root = [3,null,6,2,9,null,1,7]
Output
[3, 6, 1, 7, 2, 9]

Solve it

More in Advanced Trees

All algorithm problems