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:
[0, 100].-100 <= Node.val <= 100Follow up: Recursive solution is trivial, could you do it iteratively?
Problem Overview: You are given the root of a binary tree and need to return the inorder traversal of its nodes. Inorder traversal visits nodes in the order left → root → right. The result is typically stored in an array while traversing the tree.
Binary tree traversals are fundamental operations in tree problems. Inorder traversal is especially useful for binary search trees because it naturally produces values in sorted order.
Approach 1: Recursive Inorder Traversal (O(n) time, O(h) space)
The recursive solution directly mirrors the definition of inorder traversal. For each node, recursively traverse the left subtree, add the current node value to the result, then recursively traverse the right subtree. This naturally fits a Depth‑First Search pattern because you explore a path fully before backtracking.
The recursion call stack implicitly handles traversal state. Each function call processes one node and moves deeper into the tree until it reaches a null node. If the tree has n nodes, every node is visited exactly once, giving O(n) time complexity. The space complexity is O(h), where h is the tree height due to recursion stack frames. This approach is the most readable and usually the first implementation developers write when working with Depth‑First Search.
Approach 2: Iterative Inorder Traversal using Stack (O(n) time, O(h) space)
The iterative approach simulates recursion explicitly using a stack. Start from the root and repeatedly push nodes while moving to the left child. This step ensures the leftmost node is processed first. When you reach a null, pop the top node from the stack, add its value to the result, and then move to its right child.
This push-left / process / move-right pattern exactly reproduces inorder traversal without recursion. Every node is pushed and popped from the stack at most once, resulting in O(n) time complexity. The stack stores at most the nodes along the current path from root to leaf, so space usage remains O(h).
The iterative version is preferred when recursion depth could be large or when interviewers want to see your understanding of how traversal works internally. It demonstrates control over stack-based state management instead of relying on the call stack.
Recommended for interviews: Start with the recursive DFS version to show you understand the traversal rule (left → root → right). Then mention the iterative stack solution as the non-recursive alternative. Interviewers usually expect the iterative version for deeper understanding of traversal mechanics, while the recursive approach demonstrates clean problem decomposition.
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Space Complexity: O(n) due to the recursion stack.
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.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
| 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) |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Inorder Traversal (DFS) | O(n) | O(h) | Best for readability and quick implementation when recursion depth is manageable |
| Iterative Inorder Traversal using Stack | O(n) | O(h) | Preferred when avoiding recursion or when interviewers want explicit stack-based traversal |
Construct Binary Tree from Inorder and Preorder Traversal - Leetcode 105 - Python • NeetCode • 307,238 views views
Watch 9 more video solutions →Practice Binary Tree Inorder Traversal with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor