代码随想录算法训练营第十四天| 144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历

144. 二叉树的前序遍历:


代码思路

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

144. 二叉树的中序遍历:


代码思路

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

144. 二叉树的后序遍历:


代码思路

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

你可能感兴趣的:(代码随想录打卡,算法,leetcode,数据结构)