590. Post-order traversal of N-ary tree (java implementation) --LeetCode

topic:

Given an N-ary tree, return the post-order traversal of its node values.

For example, given a three-ary tree:
Insert picture description here

Return to the subsequent traversal: [5,6,3,2,4,1].

Explanation: The recursion method is very simple, can you use iterative method to complete this problem?

Solution 1: Recursion

    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);
    }

Time complexity: On

Space complexity: On
Insert picture description here

Solution 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;
    }

Time complexity: On^2

Space complexity: On
Insert picture description here

    /**
     * 思路:
     * 用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;
    }

Time complexity: On^2

Space complexity: On
Insert picture description here

    /**
     * 思路:
     * 用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;
    }

Time complexity: On^2

Space complexity: On
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_38783664/article/details/112691711