Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

589. N-ary Tree Preorder Traversal

Easy75.9% Acceptance
StackTreeDepth-First Search
Asked by:
G
Google
ProblemSolutions (12)VideosCompanies (1)Notes

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?

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The goal of preorder traversal in an N-ary tree is to visit the root node first, then recursively process each of its children from left to right. Because each node can have multiple children (unlike binary trees), the traversal simply extends the classic preorder DFS idea to a list of children.

A common approach is Depth-First Search (DFS) using recursion. Start at the root, record its value, and recursively traverse each child in order. This method is simple and closely follows the natural definition of preorder traversal.

An alternative is an iterative solution using a stack. Push the root node onto the stack, process it, and push its children in reverse order so they are visited in the correct left-to-right sequence. Both approaches ensure every node is visited exactly once.

The time complexity is O(n) since every node is processed once, while the space complexity depends on recursion depth or stack usage, which can reach O(h) where h is the tree height.

Complexity

ApproachTime ComplexitySpace Complexity
Recursive DFSO(n)O(h)
Iterative DFS using StackO(n)O(h)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Recursive Approach

This approach leverages the simplicity of recursion to perform a preorder traversal on an n-ary tree. We start at the root node, then recursively process each child's subtree.

Time Complexity: O(n), where n is the number of nodes in the tree, since we visit each node once.
Space Complexity: O(n) for the recursion stack used in the helper function.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4// Definition for a Node.
5struct Node {
6    int val;
7    int numChildren;
8    struct Node** children;
9};
10
11void preorderHelper(struct Node* node, int* returnArray, int* returnSize) {
12    if (node == NULL) return;
13    returnArray[(*returnSize)++] = node->val;
14    for (int i = 0; i < node->numChildren; i++) {
15        preorderHelper(node->children[i], returnArray, returnSize);
16    }
17}
18
19int* preorder(struct Node* root, int* returnSize) {
20    *returnSize = 0;
21    int* returnArray = malloc(10000 * sizeof(int)); // Assuming max size
22    preorderHelper(root, returnArray, returnSize);
23    return returnArray;
24}

Explanation

The C code defines a node structured to represent each tree node with value and children. Using a helper function, preorderHelper, it ensures the tree is traversed in preorder by accessing each node's value before recursively calling the helper on each child.

Iterative Approach with Stack

This approach uses an explicit stack to simulate the call stack of recursion. We manually manage traversals seen in recursion, starting from the root node, iteratively processing each node, then adding each child to a stack for future visits.

Time Complexity: O(n). Space Complexity: O(n), as most nodes can be stored in the stack in the worst case.

CC++JavaPythonC#JavaScript
1#









Video Solutions

Watch expert explanations and walkthroughs

Serialize and Deserialize Binary Tree - Preorder Traversal - Leetcode 297 - Python

NeetCode
13:48128,074 views

Asked By Companies

1 companies
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Related Topics

StackTreeDepth-First Search

Problem Stats

Acceptance Rate75.9%
DifficultyEasy
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is N-ary Tree Preorder Traversal asked in FAANG interviews?

Yes, tree traversal problems like N-ary Tree Preorder Traversal are common in technical interviews, including FAANG companies. They test your understanding of tree structures, recursion, and DFS traversal strategies.

What data structure is best for N-ary Tree Preorder Traversal?

A stack is commonly used for the iterative DFS approach. It helps simulate the recursion process and ensures nodes are processed in preorder. When pushing children onto the stack, they are typically added in reverse order to maintain the correct traversal sequence.

What is the optimal approach for N-ary Tree Preorder Traversal?

The optimal approach is Depth-First Search (DFS), either recursively or using an explicit stack. Both methods visit the root first and then traverse all children from left to right. Each node is processed exactly once, resulting in O(n) time complexity.

What is the difference between binary tree preorder and N-ary tree preorder?

In a binary tree, each node has at most two children (left and right), so preorder processes root, left, then right. In an N-ary tree, a node can have multiple children, so preorder processes the root and then iterates through all children from left to right.

Previous Problem

Shortest Unsorted Continuous Subarray

Next Problem

N-ary Tree Postorder Traversal

include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<stdbool.h>
4
5
// Definition for a Node.
6
struct
Node
{
7
int
val
;
8
int
numChildren
;
9
struct
Node
*
*
children
;
10
}
;
11
12
struct
NodeStack
{
13
struct
Node
*
node
;
14
struct
NodeStack
*
next
;
15
}
;
16
17
void
push
(
struct
NodeStack
*
*
stack
,
struct
Node
*
node
)
{
18
struct
NodeStack
*
new_node
=
(
struct
NodeStack
*
)
malloc
(
sizeof
(
struct
NodeStack
)
)
;
19
new_node
->
node
=
node
;
20
new_node
->
next
=
*
stack
;
21
*
stack
=
new_node
;
22
}
23
24
struct
Node
*
pop
(
struct
NodeStack
*
*
stack
)
{
25
if
(
*
stack
==
NULL
)
return
NULL
;
26
struct
NodeStack
*
temp
=
*
stack
;
27
struct
Node
*
node
=
temp
->
node
;
28
*
stack
=
(
*
stack
)
->
next
;
29
free
(
temp
)
;
30
return
node
;
31
}
32
33
bool
isEmpty
(
struct
NodeStack
*
stack
)
{
34
return
stack
==
NULL
;
35
}
36
37
int
*
preorder
(
struct
Node
*
root
,
int
*
returnSize
)
{
38
*
returnSize
=
0
;
39
if
(
!
root
)
return
NULL
;
40
41
int
*
result
=
malloc
(
10000
*
sizeof
(
int
)
)
;
42
struct
NodeStack
*
stack
=
NULL
;
43
push
(
&
stack
,
root
)
;
44
45
while
(
!
isEmpty
(
stack
)
)
{
46
struct
Node
*
node
=
pop
(
&
stack
)
;
47
result
[
(
*
returnSize
)
++
]
=
node
->
val
;
48
49
for
(
int
i
=
node
->
numChildren
-
1
;
i
>=
0
;
i
--
)
{
50
push
(
&
stack
,
node
->
children
[
i
]
)
;
51
}
52
}
53
54
return
result
;
55
}

Explanation

This C solution uses a stack structure to handle the iterative traversal. The stack manually manages nodes left to process, with each node added to the result upon visitation. It handles children in reverse order to maintain correct preorder evaluation.

Binary Tree Inorder TraversalEasy
Flatten Binary Tree to Linked ListMedium
Binary Tree Preorder TraversalEasy
Binary Tree Postorder TraversalEasy
More similar problems