Skip to main content

N-ary Tree Preorder Traversal - Video Solutions

EasyStackTreeDepth-First Search

LeetCode N-ary Tree Preorder Traversal Solution Explained - Java

Nick White
6:2542,462 views
10 video solutions available

N-ary Tree Preorder Traversal - Video Solution

Watch 10 video solutions for N-ary Tree Preorder Traversal, a easy level problem involving Stack, Tree, Depth-First Search. This walkthrough by Nick White has 42,462 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

 

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]

 

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?

Read full problem with examples

Approach Overview

Problem Overview: Given the root of an N-ary tree, return its preorder traversal. In preorder traversal, you visit the current node first, then recursively process each child from left to right.

Approach 1: Recursive DFS (Time: O(n), Space: O(h))

The most natural way to solve this problem is recursion using Depth-First Search. Start at the root, add the node's value to the result list, then recursively traverse each child in order. Because preorder requires visiting the node before its children, the operation sequence is: visit node → iterate children → recurse. Each node is processed exactly once, giving O(n) time complexity where n is the number of nodes. The recursion stack grows with the height of the tree, so the auxiliary space is O(h). This approach is concise and mirrors the definition of preorder traversal directly.

Approach 2: Iterative Traversal with Stack (Time: O(n), Space: O(n))

An iterative solution replaces recursion with an explicit stack. Push the root node onto the stack, then repeatedly pop the top node, record its value, and push its children onto the stack. The key detail is the order: push children in reverse order so the leftmost child is processed first when popped. This preserves preorder traversal order. Every node enters and leaves the stack once, so the time complexity remains O(n). The stack may contain up to O(n) nodes in the worst case (for example, a wide level in the tree). This method avoids recursion and works well in environments where recursion depth could be limited.

Recommended for interviews: The recursive DFS approach is usually expected first because it directly expresses preorder traversal and is easy to implement correctly. Interviewers often accept it as the primary solution. The iterative stack version demonstrates deeper understanding of traversal mechanics and recursion simulation, which can strengthen your answer if follow-up questions ask for an iterative implementation.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive DFSO(n)O(h)Best for clarity and typical interview solutions when recursion depth is manageable
Iterative Stack TraversalO(n)O(n)Preferred when avoiding recursion or when practicing explicit stack-based DFS