Leetcode 145 - Binary Tree Postorder Traversal

题目:
Leetcode 145 - Binary Tree Postorder Traversal
python3代码:

# 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:
    def postorderTraversal(self, root: TreeNode) -> List[int]:
        if not root:
            return list() # 或者 return [] 
        res = []
        self.postTraversal(root, res)
        return res
    
    def postTraversal(self, root, res):
        if root:
            self.postTraversal(root.left, res)
            self.postTraversal(root.right, res)
            res.append(root.val)

如果觉得不错,就点赞关注留言吧~
谢谢~

上一篇:145. 二叉树的后序遍历 + 递归实现 + 非递归实现


下一篇:1063: 马戏团