590. N叉树的后序遍历(java实现)--LeetCode

本文介绍了如何使用递归和栈两种方法实现N叉树的后序遍历。递归法直接从根节点开始,先遍历子节点再添加当前节点值;而栈法则通过根右左的顺序将节点压入栈中,最后反转结果。两种方法的时间复杂度分别为O(n)和O(n^2),空间复杂度均为O(n)。
摘要由CSDN通过智能技术生成

题目:

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

例如,给定一个 3叉树 :
在这里插入图片描述

返回其后序遍历: [5,6,3,2,4,1].

说明: 递归法很简单,你可以使用迭代法完成此题吗?

解法1:递归

    public List<Integer> postorder(Node root) {
        ArrayList<Integer> result = new ArrayList<>();
        recursive(root,result);
        return result;
    }

    private void recursive(Node root, ArrayList<Integer> result) {
        if (root==null)return;
        if (!root.children.isEmpty()){
            for (Node n:root.children){
                recursive(n,result);
            }
        }
        result.add(root.val);
    }

时间复杂度:On

空间复杂度:On
在这里插入图片描述

解法2:stack

    /**
     * 思路:
     * 用ArrayList实现
     * 根右左记录,之后反转结果集
     */
    public List<Integer> postorder(Node root) {
        ArrayList<Integer> result = new ArrayList<>();
        if (root==null)return result;
        ArrayDeque<Node> stack = new ArrayDeque<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            result.add(pop.val);
            if (!pop.children.isEmpty()){
                for (Node n:pop.children){
                    stack.push(n);
                }
            }
        }
        Collections.reverse(result);
        return result;
    }

时间复杂度:On^2

空间复杂度:On
在这里插入图片描述

    /**
     * 思路:
     * 用ArrayList实现
     * 根右左入栈,每次把children赋值为null,没有children加入到结果集
     */
    public List<Integer> postorder(Node root) {
        ArrayList<Integer> result = new ArrayList<>();
        if (root==null)return result;
        ArrayDeque<Node> stack = new ArrayDeque<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            if (pop.children!=null){
                stack.push(pop);
                Collections.reverse(pop.children);
                for (Node n:pop.children){
                    stack.push(n);
                }
                pop.children=null;
            }else {
                result.add(pop.val);
            }
        }
        return result;
    }

时间复杂度:On^2

空间复杂度:On
在这里插入图片描述

    /**
     * 思路:
     * 用LinkedList实现
     * 链表实现,根右左加入链表的头部
     */
         public List<Integer> postorder(Node root) {
        LinkedList<Integer> result = new LinkedList<>();
        if (root==null)return result;
        ArrayDeque<Node> stack = new ArrayDeque<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            result.offerFirst(pop.val);
            for (Node n:pop.children){
                stack.push(n);
            }
        }
        return result;
    }

时间复杂度:On^2

空间复杂度:On
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值