Skip to content

Latest commit

 

History

History

145-BinaryTreePostorderTraversal

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Binary Tree Postorder Traversal

Problem can be found in here!

# 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

Solution: Recursion

class Solution:
    def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        self.result = []
        self.traversalHelper(root)
        return self.result

    def traversalHelper(self, root: Optional[TreeNode]) -> None:
        if not root:
            return

        self.traversalHelper(root.left)
        self.traversalHelper(root.right)
        self.result.append(root.val)

Explanation: Postorfer traversal visits left node → right node → root

Time Complexity: O(n), Space Complexity: O(n)