2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_590.java 1005 Bytes
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.n_ary_tree;
import com.hit.common.NaryTreeNode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
/**
* 590. N-ary Tree Postorder Traversal
* <p>
* Given an n-ary tree, return the postorder traversal of its nodes' values.
* <p>
* For example, given a 3-ary tree:
* <p>
* Return its postorder traversal as: [5,6,3,2,4,1].
* <p>
* Note:
* <p>
* Recursive solution is trivial, could you do it iteratively?
*/
public class _590 {
public List<Integer> postorder(NaryTreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<NaryTreeNode> stack = new Stack<>();
stack.add(root);
while (!stack.isEmpty()) {
root = stack.pop();
list.add(root.val);
for (NaryTreeNode node : root.children)
stack.add(node);
}
Collections.reverse(list);
return list;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助