Arena / Problems /

145. Binary Tree Postorder Traversal

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #145

Given the root of a binary tree, return the postorder traversal of its nodes' values.

Follow up: Recursive solution is trivial, could you do it iteratively?

Example 1
Input: root = [1,null,2,3]
Output: [3,2,1]
Example 2
Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]
Output: [4,6,7,5,2,9,8,3,1]
Example 3
Input: root = []
Output: []
Example 4
Input: root = [1]
Output: [1]
Constraints
  • The number of the nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (2)

Peer solutions & challenges
#1 Recursion by Anonymous Developer
0
# 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]:
        self.result = []
        self.traverse(root)
        return self.result

    def traverse(self, node):
        if not node:
            return

        self.traverse(node.left)
        self.traverse(node.right)
        self.result.append(node.val)
#2 Iteration by Anonymous Developer
0
# 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]:
        if root is None:
            return []

        stack = [(root, False)] # key 1
        result = []

        while stack:
            node, visited = stack.pop()

            if visited: # key 2
                result.append(node.val)
                continue

            stack.append((node, True)) # key 3

            if node.right:
                stack.append((node.right, False))

            if node.left:
                stack.append((node.left, False))

        return result
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #145 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.