[LeetCode题解] 145. 二叉树的后序遍历

题目链接: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/.

后序遍历原则:
先遍历左子树,再遍历右子树,最后遍历根节点。

递归版本:

class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        if not root.left and not root.right:
            return [root.val]
        return self.postorderTraversal(root.left) + self.postorderTraversal(root.right) + [root.val]
发布了10 篇原创文章 · 获赞 0 · 访问量 130

猜你喜欢

转载自blog.csdn.net/m0_46134602/article/details/103819992