Skip to content

Latest commit

 

History

History
47 lines (36 loc) · 1.28 KB

_145. Binary Tree Postorder Traversal.md

File metadata and controls

47 lines (36 loc) · 1.28 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : August 25, 2024

Last updated : August 25, 2024


Related Topics : Stack, Tree, Depth-First Search, Binary Tree

Acceptance Rate : 74.95 %


Solutions

Python

# 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: Optional[TreeNode]) -> List[int]:
        def dfs(curr: Optional[TreeNode], output: List[int] = []) -> List[int] :
            if not curr :
                return output
            if curr.left :
                dfs(curr.left, output)
            if curr.right :
                dfs(curr.right, output)
            output.append(curr.val)
            return output
        return dfs(root)