N-ary Tree Preorder Traversal - Problem

Given the root of an n-ary tree, return the preorder traversal of its nodes' values.

In preorder traversal, we visit nodes in this order:

  1. Visit the current node (process its value)
  2. Recursively visit all children from left to right

An n-ary tree is a tree where each node can have any number of children (0 to n), unlike binary trees which have at most 2 children.

Input Format: The tree is represented using level order traversal where each group of children is separated by null values.

Goal: Return a list containing all node values in preorder sequence.

Input & Output

example_1.py โ€” Simple N-ary Tree
$ Input: root = [1,null,3,2,4,null,5,6]
โ€บ Output: [1,3,5,6,2,4]
๐Ÿ’ก Note: Starting at root 1, we visit it first. Then we visit its children 3,2,4 in order. For node 3, we first visit 3 itself, then its children 5,6. Finally we visit nodes 2 and 4 (which have no children).
example_2.py โ€” Deeper Tree
$ 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]
๐Ÿ’ก Note: This demonstrates preorder traversal on a more complex tree with multiple levels. We always visit parent nodes before their children, going depth-first from left to right.
example_3.py โ€” Single Node
$ Input: root = [1]
โ€บ Output: [1]
๐Ÿ’ก Note: Edge case with just one node. The preorder traversal simply returns that single node's value.

Constraints

  • The number of nodes in the tree is in the range [0, 104]
  • 0 โ‰ค Node.val โ‰ค 104
  • The height of the n-ary tree is less than or equal to 1000
  • Follow up: Recursive solution is trivial, could you do it iteratively?

Visualization

Tap to expand
1โ‘ 3โ‘ก2โ‘ค4โ‘ฅ5โ‘ข6โ‘ฃTraversal PathPreorder Result: [1, 3, 5, 6, 2, 4]๐ŸŽฏ Pattern: Visit parent first, then children left-to-rightโšก Time: O(n) - visit each node once๐Ÿ“ฆ Space: O(h) recursion stack or O(w) iterative stackProcessing Orderโ‘  Visit root: 1โ‘ก Visit child: 3โ‘ข Visit grandchild: 5โ‘ฃ Visit grandchild: 6โ‘ค Visit child: 2โ‘ฅ Visit child: 4
Understanding the Visualization
1
Start at Root
Begin at the root node and add its value to the result immediately
2
Visit Children Left-to-Right
Process each child recursively in order from left to right
3
Depth-First Pattern
For each child, complete its entire subtree before moving to the next sibling
4
Build Result
The final result contains all nodes in preorder: parent before children
Key Takeaway
๐ŸŽฏ Key Insight: Preorder traversal naturally follows parent-child relationships - we always process a node before exploring its descendants. Both recursive and iterative solutions are optimal with O(n) time complexity, visiting each node exactly once.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28 Apple 22
125.0K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen