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:
vector<int> ans;
vector<int> postorder(Node* root) {
dfs(root);
return ans;
}
void dfs(Node* root) {
if (!root) return ;
for (auto t : root->children)
dfs(t);
ans.push_back(root->val);
}
};