1525. N叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历。
样例
样例
输入 : {1,3,2,4#2#3,5,6#4#5#6}
输出: [5,6,3,2,4,1]
说明:
这棵树如下所示(左侧的)
/**
* Definition for Undirected graph.
* class UndirectedGraphNode {
* int label;
* List<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) {
* label = x;
* neighbors = new ArrayList<UndirectedGraphNode>();
* }
* }
*/
public class Solution {
/**
* @param root: the root of the tree
* @return: post order of the tree
*/
public List<Integer> postorder(UndirectedGraphNode root) {
// write your code here
if (root!=null){
for (int i = 0; i < root.neighbors.size(); i++) {
postorder(root.neighbors.get(i));
}
result.add(root.label);
}
return result;
}
List<Integer> result=new ArrayList<>();
}