给定一个 N 叉树,返回其节点值的后序遍历。
样例
样例
输入 : {1,3,2,4#2#3,5,6#4#5#6}
输出: [5,6,3,2,4,1]
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
/**
* @param root: the root of the tree
* @return: post order of the tree
*/
vector<int> postorder(UndirectedGraphNode* root) {
// write your code here
vector<int> res;
recursion(root,res);
return res;
}
void recursion(UndirectedGraphNode* root,vector<int> &res)
{
if(root==NULL) return;
for (int i = 0; i < root->neighbors.size(); i++) {
/* code */
recursion(root->neighbors[i],res);
}
res.push_back(root->label);
}
};