给定一个 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> postorder(Node* root)
{
if(root == NULL)
return res;
Fun(root);
return res;
}
void Fun(Node* root)
{
if(root == NULL)
return;
int len = root ->children.size();
for(int i = 0; i < len; i++)
{
if(root ->children[i] != NULL)
Fun(root ->children[i]);
}
res.push_back(root ->val);
}
};
本文介绍了一种实现N叉树后序遍历的方法。通过递归方式遍历每个子节点并最后处理根节点,确保了遍历顺序正确。文章提供了一个具体的C++实现示例。
474

被折叠的 条评论
为什么被折叠?



