LeetCode刷题实战589:N 叉树的前序遍历
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)
示例

解题
class Solution {
public:
vector<int> num;
vector<int> preorder(Node* root) {
if(root==NULL) return num; //特例
num.emplace_back(root->val); //加入元素
//前序遍历
for(Node* t : root->children)
{
preorder(t);
}
return num;
}
};
评论