Binary Tree Inorder Traversal - Solution & Explanation
Problem Statement
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Explanation:

Example 2:
Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]
Output: [4,2,6,5,7,1,3,9,8]
Explanation:

Example 3:
Input: root = []
Output: []
Example 4:
Input: root = [1]
Output: [1]
Constraints:
- The number of 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 inorder traversal of its nodes' values. Inorder means you always visit the left subtree, then the current node, then the right subtree.
Approach 1: Recursive Inorder Traversal (O(n) time, O(h) space)
This approach uses recursion to follow the natural definition of inorder traversal. Starting from the root, recursively traverse the left child, append the current node value to the result array, then recursively traverse the right child. The recursion stack implicitly handles the traversal order. Each node is visited exactly once, giving O(n) time complexity. The space complexity is O(h), where h is the height of the tree, due to the recursion call stack. This method is the cleanest and easiest to implement when working with tree traversal problems.
Approach 2: Iterative Inorder Traversal Using Stack (O(n) time, O(h) space)
This method simulates recursion using an explicit stack. Start from the root and repeatedly push nodes while moving to the leftmost child. When no more left children exist, pop the top node from the stack, record its value, and move to its right child. Repeat the process until both the stack is empty and the current node is null. Each node is pushed and popped at most once, so the traversal runs in O(n) time. The stack holds at most the height of the tree, giving O(h) space. This technique is common in iterative depth-first search implementations.
Recommended for interviews: Interviewers expect you to know both approaches. The recursive version shows you understand tree traversal fundamentals and DFS ordering. The iterative stack solution demonstrates stronger control over execution flow and the ability to convert recursion into an explicit data structure. Many interviewers ask for the iterative version after seeing the recursive solution.
Approach 1: Recursive Inorder Traversal
This approach uses recursion to traverse the binary tree. Inorder traversal involves visiting the left subtree, the root node, and then the right subtree. The base case for the recursion is to return if the current node is null.
The C solution defines a helper function inorderTraversalHelper to perform the recursion. The function is called with the current node and a result array to store the values. This implementation assumes a maximum of 100 nodes and allocates space accordingly, which is suitable for the problem constraints but would require dynamic reallocation in a more general scenario.
Complexity
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Space Complexity: O(n) due to the recursion stack.
Approach 2: Iterative Inorder Traversal
In this approach, we use a stack to perform an iterative inorder traversal. The stack is utilized to track the nodes to be visited. This method mimics the recursive behavior by explicitly using a stack to push left children until reaching a null entry, then processes the nodes and explores the right subtrees.
The C implementation employs a manual stack data structure to facilitate the iterative traversal. Nodes are pushed onto the stack until null is reached, allowing us to backtrack, visit the node, and repeat the process for the right subtree.
Complexity
Time Complexity: O(n)
Space Complexity: O(n)
Approach 3: Recursive Traversal
We first recursively traverse the left subtree, then visit the root node, and finally recursively traverse the right subtree.
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree, and the space complexity mainly depends on the stack space of the recursive call.
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Approach 4: Stack Implementation for Non-recursive Traversal
The non-recursive approach is as follows:
- Define a stack
stk. - Push the left nodes of the tree into the stack in sequence.
- When the left node is null, pop and process the top element of the stack.
- Repeat steps 2-3.
The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree, and the space complexity mainly depends on the stack space.
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
Approach 5: Morris Implementation for In-order Traversal
Morris traversal does not require a stack, so the space complexity is O(1). The core idea is:
Traverse the binary tree nodes,
- If the left subtree of the current node
rootis null, add the current node value to the result listans, and update the current node toroot.right. - If the left subtree of the current node
rootis not null, find the rightmost nodeprevof the left subtree (which is the predecessor node of therootnode in in-order traversal):- If the right subtree of the predecessor node
previs null, point the right subtree of the predecessor node to the current noderoot, and update the current node toroot.left. - If the right subtree of the predecessor node
previs not null, add the current node value to the result listans, then point the right subtree of the predecessor node to null (i.e., disconnectprevandroot), and update the current node toroot.right.
- If the right subtree of the predecessor node
- Repeat the above steps until the binary tree node is null, and the traversal ends.
The time complexity is O(n), and the space complexity is O(1). Here, n is the number of nodes in the binary tree.
Code
Python
Java
C++
Go
TypeScript
JavaScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Recursive Inorder Traversal | Time Complexity: O(n), where n is the number of nodes in the binary tree. Space Complexity: O(n) due to the recursion stack. |
| Iterative Inorder Traversal | Time Complexity: O(n) Space Complexity: O(n) |
| Recursive Traversal | — |
| Stack Implementation for Non-recursive Traversal | — |
| Morris Implementation for In-order Traversal | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Inorder Traversal | O(n) | O(h) | Best for readability and quick implementation when recursion is allowed |
| Iterative Traversal with Stack | O(n) | O(h) | Preferred when avoiding recursion or when interviewers ask for iterative DFS |
Video Solution
Iterative & Recursive - Binary Tree Inorder Traversal - Leetcode 94 - Python • NeetCode • 158,573 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Binary Tree Inorder Traversal easy or hard?
How to solve Binary Tree Inorder Traversal in O(n)?
Binary Tree Inorder Traversal Python or Java solution?
What is the best approach for Binary Tree Inorder Traversal?
Is Binary Tree Inorder Traversal asked at Google/Amazon/Meta?
What data structure is used in Binary Tree Inorder Traversal?
What is the time complexity of Binary Tree Inorder Traversal?
Ready to solve this problem?
Practice Binary Tree Inorder Traversal with our built-in code editor and test cases.
Practice on FleetCode