给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 3叉树 :
返回其后序遍历: [5,6,3,2,4,1].
/*
// 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 {
private:
vector<int>res;
public:
vector<int> postorder(Node* root) {
if(!root){
return res;
}
if(!root->children.empty()){
for(auto nodes:root->children){
postorder(nodes);
}
}
res.push_back(root->val);
return res;
}
};
作者:he-zi-11
链接:https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/solution/cdi-gui-by-he-zi-11/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。