原题链接:Leecode 590. N 叉树的后序遍历
递归
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
void post(Node* root,vector<int>& r)
{
for(int i=0;i<root->children.size();i++)
{
post(root->children[i],r);
}
r.push_back(root->val);
}
vector<int> postorder(Node* root) {
if(!root) return {};
vector<int> r;
post(root,r);
return r;
}
};