LeetCode 590. 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)

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [5,6,3,2,4,1]

Example 2:

Input: 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]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • 0 <= Node.val <= 104
  • The height of the n-ary tree is less than or equal to 1000.

Follow up: Recursive solution is trivial, could you do it iteratively?


递归,也是很简单,只是变成了先遍历children再加root.val。

/*
// 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> result = new ArrayList<>();
        helper(root, result);
        return result;
    }
    
    public void helper(Node root, List<Integer> result) {
        if (root == null) {
            return;
        }
        for (Node node : root.children) {
            helper(node, result);
        }
        result.add(root.val);
    }
}

迭代,还是记着Stack + Set的方法呢,但是,debug了巨久都没搞出来,大概是seen set的地方弄错了。想着应该要把所有children都seen了才能pop它,但是刚开始想的是seen表示这个node有没有见过,于是要check所有children都seen了还挺麻烦,也不太确定这个思路对不对,就去看了答案。居然几乎没什么人跟我一个思路,看了一个相似的代码,才确认了确实是只有当node没有chidlren或者children全都seen过了才能pop,于是就把seen的含义变成了这个node的所有children是不是都seen过,调整了seen.add的位置,代码就很简洁也一下就没bug了。也算是加深了对postorder iterative的理解。

/*
// 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> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Deque<Node> stack = new ArrayDeque<>();
        Set<Node> seen = new HashSet<>();  // this node's children are all seen
        stack.push(root);
        while (!stack.isEmpty()) {
            Node node = stack.peek();
            int size = node.children.size();
            if (size == 0 || seen.contains(node)) {
                node = stack.pop();
                result.add(node.val);
            } else {
                for (int i = size - 1; i >= 0; i--) {
                    Node child = node.children.get(i);
                    if (!seen.contains(child)) {
                        stack.push(child);
                    }
                }
                seen.add(node);
            }
        }
        return result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值