算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
今天和大家聊的问题叫做 N 叉树的后序遍历,我们先来看题面:
https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/
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)
给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。
n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。
示例

解题
class Solution {
//存放结果集
List<Integer> res = new ArrayList<>();
public List<Integer> postorder(Node root) {
if (root == null) return res;
for (Node child : root.children) {
postorder(child);
}
//后序遍历
res.add(root.val);
return res;
}
}
好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。
上期推文:

3168

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



