#589

N-ary Tree Preorder Traversal

newbie · 210 · lc easy +16 · verified · 76.7% accepted · 3,262 likes · top 88%

play →

Description

Given the root of an N-ary tree, return the list of node values visited in preorder (root first, then children from left to right). The tree is serialized in level-order with null separating each node's children.

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]

Example 2:

Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]

Code

1
2
3
4
5
6
7
8
9
10
11