N-ary Tree Postorder Traversal

Easy

Description

Given the root of an n-ary tree, return the postorder traversal of its nodes values. Postorder visits all children first, then the root.

Examples

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

Postorder: children, then root.

Input:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10]
Output:[6,7,2,8,3,9,10,4,5,1]
Explanation:

In postorder traversal, all children are visited before the parent. Node 2 has children [6,7], node 3 has child [8], and node 4 has children [9,10]. Order: 6,7 (children of 2), then 2, then 8 (child of 3), then 3, then 9,10 (children of 4), then 4, then 5 (leaf), and finally root 1.

Input:root = []
Output:[]
Explanation:

An empty tree has no nodes to traverse, so the postorder traversal returns an empty array. This represents the edge case where the tree is completely empty.

Constraints

  • 0 ≤ nodes ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!