Problem
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)
Example 1:
1 | Input: root = [1,null,3,2,4,null,5,6] |
Example 2:
1 | 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] |
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?
Analysis
N叉树的前序遍历,树的遍历无非就是递归和循环。对于二叉树来说,递归的解法更加简单清晰,但是对于N叉树来说,使用循环可能比较容易。其实思路与二叉树的一样的,还是借助栈来做。
每处理一个节点时,把它的子节点都压入栈中,但是这里注意顺序,因为遍历的顺序是从左到右,所以压入的顺序是从右到左。按照这个逻辑处理直至栈中没有节点即可。
Solution
无
Code
1 | /* |
Summary
这道题目本质上考察的是二叉树的前序遍历非递归实现,原理是使用栈来解决。N叉树和二叉树处理起来没有什么不同。这道题目的分享到这里,感谢你的支持!