N-ary Tree Postorder Traversal - Problem
Given the root of an n-ary tree, return the postorder traversal of its nodes' values.
N-ary tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (see examples).
In postorder traversal, we visit all children nodes first, then the parent node.
Input & Output
Example 1 — Basic N-ary Tree
$
Input:
root = [1,null,3,2,4,null,5,6]
›
Output:
[5,6,3,2,4,1]
💡 Note:
Postorder visits children first: node 3's children [5,6], then node 3, then nodes 2,4, finally root 1
Example 2 — 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:
[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
💡 Note:
Complex tree with multiple levels - each subtree processed before its parent
Example 3 — Single Node
$
Input:
root = [1]
›
Output:
[1]
💡 Note:
Single node tree returns just that 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
Visualization
Tap to expand
Understanding the Visualization
1
Input Tree
N-ary tree with root 1 and children [3,2,4], where 3 has children [5,6]
2
Traversal Order
Visit children first: 5,6 → 3 → 2,4 → 1
3
Result
Postorder sequence: [5,6,3,2,4,1]
Key Takeaway
🎯 Key Insight: Postorder means 'children first, parent last' - perfect for bottom-up tree processing
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code