You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an n-ary tree, return the preorder traversal of its nodes' values.
For example, given a 3-ary tree:
Return its preorder traversal as: [1,3,5,6,2,4].
Note:
Recursive solution is trivial, could you do it iteratively?
这道题让我们求N叉树的前序遍历,有之前那道Binary Tree Preorder Traversal的基础,知道了二叉树的前序遍历的方法,很容易就可以写出N叉树的前序遍历。先来看递归的解法,主要实现一个递归函数即可,判空之后,将当前结点值加入结果res中,然后遍历子结点数组中所有的结点,对每个结点都调用递归函数即可,参见代码如下:
Given an n-ary tree, return the preorder traversal of its nodes' values.
For example, given a
3-ary
tree:Return its preorder traversal as:
[1,3,5,6,2,4]
.Note:
Recursive solution is trivial, could you do it iteratively?
这道题让我们求N叉树的前序遍历,有之前那道Binary Tree Preorder Traversal的基础,知道了二叉树的前序遍历的方法,很容易就可以写出N叉树的前序遍历。先来看递归的解法,主要实现一个递归函数即可,判空之后,将当前结点值加入结果res中,然后遍历子结点数组中所有的结点,对每个结点都调用递归函数即可,参见代码如下:
解法一:
我们也可以使用迭代的解法来做,使用栈stack来辅助,需要注意的是,如果使用栈的话,我们遍历子结点数组的顺序应该是从后往前的,因为栈是后进先出的顺序,所以需要最先遍历的子结点应该最后进栈,参见代码如下:
解法二:
类似题目:
Binary Tree Preorder Traversal
N-ary Tree Level Order Traversal
N-ary Tree Postorder Traversal
参考资料:
https://leetcode.com/problems/n-ary-tree-preorder-traversal/
https://leetcode.com/problems/n-ary-tree-preorder-traversal/discuss/147955/Java-Iterative-and-Recursive-Solutions
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: