版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iov3Rain/article/details/83550219
给定一个 N 叉树,返回其节点值的前序遍历。
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
//递归
class Solution {
public:
vector<int> res;
vector<int> preorder(Node* root) {
if(root == NULL)
return res;
GetAns(root);
return res;
}
void GetAns(Node* root)
{
if(root == NULL)
return;
res.push_back(root ->val);
int len = root ->children.size();
for(int i = 0; i < len; i++)
{
GetAns(root ->children[i]);
}
}
};