Skip to main content

Binary Tree Postorder Traversal - Solution & Explanation

EasyStackTreeDepth-First SearchBinary Tree31 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

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

 

Example 1:

Input: root = [1,null,2,3]

Output: [3,2,1]

Explanation:

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]

Explanation:

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

 

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

Approach Overview

Problem Overview: Given the root of a binary tree, return the nodes in postorder traversal. Postorder means visiting the left subtree, then the right subtree, and finally the current node. The challenge is producing this order efficiently using either recursion or an explicit stack.

Approach 1: Recursive Postorder Traversal (O(n) time, O(h) space)

The recursive solution mirrors the definition of postorder traversal. Start a depth‑first search from the root. Recursively process the left child, then recursively process the right child, and finally append the current node's value to the result list. Each node is visited exactly once, so the total time complexity is O(n). The space complexity is O(h), where h is the height of the tree, due to the recursion call stack. This approach is concise and easy to reason about, which makes it a common baseline when working with tree traversal problems and depth-first search patterns.

Approach 2: Iterative Postorder Traversal using Two Stacks (O(n) time, O(n) space)

The iterative method avoids recursion by using explicit stack structures. Push the root into the first stack. Repeatedly pop a node, push it into a second stack, then push its left and right children into the first stack. This process effectively creates a modified preorder traversal (root → right → left). When you pop all nodes from the second stack, the order becomes left → right → root, which is exactly postorder. Every node moves through the stacks once, giving O(n) time complexity. Space complexity becomes O(n) because both stacks may store many nodes.

The key insight behind the two‑stack approach is reversing a traversal order. Preorder normally produces root → left → right. By pushing children in a specific order and reversing the result through another stack, you transform it into postorder. This technique appears frequently in iterative binary tree traversal implementations when recursion is not preferred.

Recommended for interviews: The recursive solution is usually the first answer interviewers expect because it directly follows the DFS definition of postorder traversal and is easy to implement correctly. After presenting it, showing the iterative two‑stack approach demonstrates deeper understanding of stack-based tree traversal and how recursion can be simulated with explicit data structures.

Approach 1: Recursive Postorder Traversal

The simplest way to perform a postorder traversal of a binary tree is recursively. In postorder traversal, you need to traverse the left subtree, then traverse the right subtree, finally visit the root node. This means visiting the left child, then the right child, and then the node itself for each node in the tree.

This C solution uses a helper function, postorderHelper, to perform the traversal recursively. It first processes the left child, then the right child, and finally the root node, appending each value to the result array. This order ensures a postorder traversal, which visits each node in the left-right-root order.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of nodes in the binary tree, because each node is visited once.
Space Complexity: O(h), where h is the height of the tree, due to recursive call stack usage.

Try this approach in the editor →

Approach 2: Iterative Postorder Traversal using Two Stacks

To perform postorder traversal iteratively, two stacks can be used. The first stack is used to perform a modified preorder traversal (root-right-left), while the second stack reverses this order to provide the postorder traversal (left-right-root). This approach allows the sequence of visiting nodes in postorder traversal without recursion.

This C implementation uses two stacks: the first stack (stack1) performs a modified preorder traversal (root-right-left); the second stack (stack2) is used to reverse the node visit order to achieve postorder (left-right-root) traversal. Values are ultimately extracted from the second stack.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the number of nodes.
Space Complexity: O(n) due to the usage of two stacks, each containing n nodes in the worst case for balanced or full trees.

Try this approach in the editor →

Approach 3: Recursion

We first recursively traverse the left and right subtrees, then visit the root node.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree. The space complexity mainly depends on the stack space used for recursive calls.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Approach 4: Stack Implementation for Postorder Traversal

The order of preorder traversal is: root, left, right. If we change the order of the left and right children, the order becomes: root, right, left. Finally, reversing the result gives us the postorder traversal result.

Therefore, the idea of using a stack to implement non-recursive traversal is as follows:

  1. Define a stack stk, and first push the root node into the stack.
  2. If the stack is not empty, pop a node from the stack each time.
  3. Process the node.
  4. First push the left child of the node into the stack, then push the right child of the node into the stack (if there are child nodes).
  5. Repeat steps 2-4.
  6. Reverse the result to get the postorder traversal result.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree. The space complexity mainly depends on the stack space.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Approach 5: Morris Implementation for Postorder Traversal

Morris traversal does not require a stack, and its space complexity is O(1). The core idea is:

Traverse the binary tree nodes,

  1. If the right subtree of the current node root is empty, add the current node value to the result list ans, and update the current node to root.left.
  2. If the right subtree of the current node root is not empty, find the leftmost node next of the right subtree (which is the successor of the root node in inorder traversal):
    • If the left subtree of the successor node next is empty, add the current node value to the result list ans, then point the left subtree of the successor node to the current node root, and update the current node to root.right.
    • If the left subtree of the successor node next is not empty, point the left subtree of the successor node to null (i.e., disconnect next and root), and update the current node to root.left.
  3. Repeat the above steps until the binary tree node is null, and the traversal ends.
  4. Finally, return the reverse of the result list.

The idea of Morris postorder traversal is consistent with Morris preorder traversal, just change the "root-left-right" of preorder to "root-right-left", and finally reverse the result to become "left-right-root".

The time complexity is O(n), where n is the number of nodes in the binary tree. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Recursive Postorder Traversal

Time Complexity: O(n), where n is the number of nodes in the binary tree, because each node is visited once.
Space Complexity: O(h), where h is the height of the tree, due to recursive call stack usage.

Iterative Postorder Traversal using Two Stacks

Time Complexity: O(n) where n is the number of nodes.
Space Complexity: O(n) due to the usage of two stacks, each containing n nodes in the worst case for balanced or full trees.

Recursion
Stack Implementation for Postorder Traversal
Morris Implementation for Postorder Traversal

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive Postorder TraversalO(n)O(h)Best for clarity and interviews when recursion is acceptable
Iterative Traversal using Two StacksO(n)O(n)When avoiding recursion or demonstrating stack-based traversal logic

Video Solution

Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - PythonNeetCodeIO60,083 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Binary Tree Postorder Traversal easy or hard?
Binary Tree Postorder Traversal is considered an Easy problem because the recursive solution directly follows the definition of postorder traversal. The iterative stack-based implementation is slightly more involved but still manageable once you understand DFS traversal patterns.
Binary Tree Postorder Traversal Python/Java solution
In Python or Java, the typical solution uses recursion with a helper DFS function. The function recursively processes the left child, then the right child, and appends the current node value to a list. This produces the postorder sequence in O(n) time.
How to solve Binary Tree Postorder Traversal in O(n)?
Perform a depth-first traversal that processes nodes in the order left → right → root. A recursive DFS naturally achieves this in O(n) time. An iterative alternative uses two stacks: push nodes from the first stack into the second while adding children, then pop from the second stack to produce the correct postorder sequence.
What is the best approach for Binary Tree Postorder Traversal?
The recursive depth‑first search approach is the most straightforward solution. Traverse the left subtree, then the right subtree, and finally process the node itself. This visits every node once with O(n) time complexity and O(h) space for the recursion stack, where h is the tree height.
Is Binary Tree Postorder Traversal asked at Google/Amazon/Meta?
Binary tree traversal problems frequently appear in interviews at companies like Amazon, Google, and Meta. Variants of postorder traversal are commonly used to test understanding of recursion, DFS, and stack-based tree traversal techniques.
What data structure is used in Binary Tree Postorder Traversal?
Traversal relies on recursion or an explicit stack. Recursive solutions use the call stack implicitly during DFS, while iterative approaches use one or two stack data structures to simulate the traversal order.
What is the time complexity of Binary Tree Postorder Traversal?
Binary Tree Postorder Traversal runs in O(n) time because each node in the tree is visited exactly once. Recursive implementations use O(h) auxiliary space from the call stack, while iterative implementations using stacks may use up to O(n) space in the worst case.

Ready to solve this problem?

Practice Binary Tree Postorder Traversal with our built-in code editor and test cases.

Practice on FleetCode