LeetCode刷题实战590:N 叉树的后序遍历
程序IT圈
共 1095字,需浏览 3分钟
· 2022-04-26
Given the root of an n-ary tree, return the postorder traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
示例
![](https://filescdn.proginn.com/70720e3cbd5e2b7bb3f328f1eaee49a5/4ee95491979ae2d5a8fbe71101477922.webp)
解题
class Solution {
//存放结果集
Listres = new ArrayList<>();
public Listpostorder(Node root) {
if (root == null) return res;
for (Node child : root.children) {
postorder(child);
}
//后序遍历
res.add(root.val);
return res;
}
}
评论