LeetCode 590: N 叉树的后序遍历 N-ary Tree Postorder Traversal

题目:

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

Given an n-ary tree, return the postorder traversal of its nodes' values.

示例 1:


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

示例 2:

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

输入: 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]

说明:

  1. 树的深度不会超过 1000

  2. 树的节点总数不会超过 10000

Constraints:

  • The height of the n-ary tree is less than or equal to 1000

  • The total number of nodes is between [0, 10^4]

进阶:

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

Follow up:

Recursive solution is trivial, could you do it iteratively?

解题思路:

N 叉树的前序, 中序, 后序遍历 本质上就是深度优先搜索的不同表现形式 , 既然是深度优先搜索, 那么理论上都可以用递归或栈迭代来解题. 详情可以看之前的文章:

队列和 BFS, 栈和 DFS

树的遍历    Traverse a Tree

递归法:

Java:

class Solution {
    List<Integer> res = new ArrayList<>();

    public List<Integer> postorder(Node root) {
        if (root == null) // 基线条件
            return res;
        for (Node node : root.children) // 遍历子结点
            postorder(node); // 递归子结点
        res.add(root.val); // 存储结点值
        return res;
    }
}

Python:

class Solution:
    def __init__(self):
        self.res = list() # 初始化返回数组

    def postorder(self, root: 'Node') -> List[int]:
        if not root: # 基线条件
            return self.res
        for node in root.children: # 遍历子结点
            self.postorder(node) # 递归子结点
        self.res.append(root.val) # 存储结点值
        return self.res

迭代法:

Java:

class Solution {
    LinkedList<Integer> res = new LinkedList<>();
    Stack<Node> stack = new Stack<>();

    public List<Integer> postorder(Node root) {
        if (root == null)
            return res;
        stack.add(root);
        while (!stack.isEmpty()) { // 栈不空,遍历不止
            Node node = stack.pop(); // 栈顶元素出栈
            res.addFirst(node.val); // 存储结点值到数组头部
            for (Node temp : node.children) // 遍历子结点
                stack.add(temp); // 入栈
        }
        return res;
    }
}

由于栈先进先出的特性, 这里使用 res.addFirst(node.val); 应该存储结点值到数组头部

也可以改为 res.add(node.val); 存储结点值到数组尾部, 这时 Collections.reverse(res); 返回反转后的数组 res

Python 同理

Python:

class Solution:
    def postorder(self, root: 'Node') -> List[int]:
        res = list()
        if not root:
            return res
        stack = collections.deque() # 初始化双端队列
        stack.append(root) # 根结点入栈
        while stack: # 栈不空,遍历不止
            node = stack.pop() # 栈顶元素出栈
            res.append(node.val) #  存储结点值
            if node.children:
                for cld in node.children: # 遍历子结点
                    stack.append(cld) # 入栈子结点
        return res[::-1] # 返回反转后的数组 res
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值