Welcome to the fascinating world of N-ary trees! Unlike binary trees that have at most 2 children per node, N-ary trees can have any number of children - making them perfect for representing hierarchical structures like file systems, organizational charts, or family trees.
Your mission is to perform a postorder traversal of an N-ary tree and return the values in the correct order. In postorder traversal, we visit all children nodes first, then the parent node - think of it as exploring all rooms in a house before examining the main entrance.
What you're given:
- The
rootof an N-ary tree - Each node can have 0 to N children
What you need to return:
- A list containing all node values in postorder traversal sequence
Note: The tree input uses level-order serialization where each group of children is separated by null values.
Input & Output
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?