0589. Preorder Traversal
Easy | Tree + Recursion / Stack | 32 ms (98.74%), 16.4 MB (75.58%)
Last updated
Was this helpful?
Easy | Tree + Recursion / Stack | 32 ms (98.74%), 16.4 MB (75.58%)
Last updated
Was this helpful?
Source: GitHub:
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?
Excepting the above hints, we append each node's value first before visiting the child nodes.