【力扣每日一题】力扣590N叉树的后序遍历

题目来源

力扣590N叉树的后序遍历

题目概述

给定一个 n 叉树的根节点 root ,返回 其节点值的 后序遍历 。

思路分析

前面几篇博客讲过了二叉树和N叉树的各种遍历方式。这道题目也是一样的。

代码实现

public class Solution {
    List res = new ArrayList<>();
    public List postorder(Node root) {
        if (root == null) {
            return res;
        }
        if (root.children != null) {
            for (Node child : root.children) {
                postorder(child);
            }
        }
        res.add(root.val);
        return res;
    }
}

你可能感兴趣的:(leetcode,算法,java)