Leetcode-590. N 叉树的后序遍历

题目:

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

n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。

示例 1:

复制代码
输入:root = [1,null,3,2,4,null,5,6]
输出:[5,6,3,2,4,1]

示例 2:

复制代码
输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:[2,6,14,11,7,3,12,8,4,13,9,10,5,1]

提示:

  • 节点总数在范围 [0, 10^4]
  • 0 <= Node.val <= 10^4
  • n 叉树的高度小于或等于 1000

直接上代码,先序中序后序遍历,都差不多,只是加入值的位置不同而已。

java 复制代码
/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
    public List<Integer> postorder(Node root) {
        List<Integer> res = new ArrayList<>();
        digui(root,res);
        return res;
    }

    public void digui(Node root,List<Integer> res){
        if(root!=null){
            for(Node child:root.children){
                digui(child,res);
            }
            res.add(root.val);
        }
    }
}
相关推荐
两个蝴蝶飞1 小时前
Java量化系列(四):实现自选股票维护功能
java·经验分享
短剑重铸之日3 小时前
7天读懂MySQL|Day 5:执行引擎与SQL优化
java·数据库·sql·mysql·架构
酒九鸠玖3 小时前
Java--多线程
java
Dreamboat-L3 小时前
云服务器上部署nginx
java·服务器·nginx
长安er3 小时前
LeetCode215/347/295 堆相关理论与题目
java·数据结构·算法·leetcode·
元亓亓亓3 小时前
LeetCode热题100--62. 不同路径--中等
算法·leetcode·职场和发展
小白菜又菜4 小时前
Leetcode 1925. Count Square Sum Triples
算法·leetcode
cici158744 小时前
C#实现三菱PLC通信
java·网络·c#
登山人在路上5 小时前
Nginx三种会话保持算法对比
算法·哈希算法·散列表
写代码的小球5 小时前
C++计算器(学生版)
c++·算法