Leetcode 二叉树的后序遍历

0 题目描述

Leetcode原题链接:二叉树的后序遍历
在这里插入图片描述

二叉树的后序遍历:按照访问左子树——右子树——根节点的方式遍历这棵树,而在访问左子树或者右子树的时候,我们按照同样的方式遍历,直到遍历完整棵树。因此整个遍历过程天然具有递归的性质,我们可以直接用递归函数来模拟这一过程。

1 递归解法

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if not root: 
            return []
        return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]

算法复杂度
时间复杂度: O ( n ) O(n) O(n),其中 n n n 为二叉树节点的个数。二叉树的遍历中每个节点会被访问一次且只会被访问一次。
空间复杂度: O ( n ) O(n) O(n)。空间复杂度取决于递归的栈深度,而栈深度在二叉树为一条链的情况下会达到 O ( n ) O(n) O(n)的级别。

2 迭代解法(堆栈)

我们也可以用迭代的方式实现方法一的递归函数,两种方式是等价的,区别在于递归的时候隐式地维护了一个栈,而我们在迭代的时候需要显式地将这个栈模拟出来,其余的实现与细节都相同,具体可以参考下面的代码。

class Solution:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        White, Gray = 0, 1
        res = []
        stack = [(White, root)]
        while stack:
            color, node = stack.pop()
            if not node: continue
            if color == White:
                stack.append((Gray, node))
                stack.append((White, node.right))
                stack.append((White, node.left))
            else:
                res.append(node.val)
        return res

算法复杂度
时间复杂度:访问每个节点恰好一次,时间复杂度为 O ( N ) O(N) O(N),其中 N N N是节点的个数,也就是树的大小。
空间复杂度:取决于树的结构,最坏情况存储整棵树,因此空间复杂度是 O ( N ) O(N) O(N)

3 Morris 后序遍历

有一种巧妙的方法可以在线性时间内,只占用常数空间来实现后序遍历。这种方法即Morris遍历。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        def addPath(node: TreeNode):
            count = 0
            while node:
                count += 1
                res.append(node.val)
                node = node.right
            i, j = len(res) - count, len(res) - 1
            while i < j:
                res[i], res[j] = res[j], res[i]
                i += 1
                j -= 1

        if not root: return []
        node, res = root, []
        while node:
            pre = node.left
            if pre:
                while pre.right and pre.right is not node:
                    pre = pre.right
                if not pre.right:
                    pre.right = node
                    node = node.left
                    continue
                else:
                    pre.right = None
                    addPath(node.left)
            node = node.right

        addPath(root)
        return res

算法复杂度
时间复杂度: O ( n ) O(n) O(n),其中 n n n是二叉树的节点数。没有左子树的节点只被访问一次,有左子树的节点被访问两次。
空间复杂度: O ( 1 ) O(1) O(1)。只操作已经存在的指针(树的空闲指针),因此只需要常数的额外空间。

参考资料

二叉树的前序遍历
二叉树的中序遍历 – Python实现三种解法

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值