0589. Preorder Traversal
Easy | Tree + Recursion / Stack | 32 ms (98.74%), 16.4 MB (75.58%)
Source: LeetCode - N-ary Tree Preorder Traversal GitHub: Solution / Performance
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)
Follow up: Recursive solution is trivial, could you do it iteratively?
For recursive solution, visiting the children (list of nodes) in the right order.
For iterative solution, visiting the children (list of nodes) reversely.
Excepting the above hints, we append each node's value first before visiting the child nodes.
Last updated