Given the root of a binary tree, return the preorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,2,3]
Explanation:

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

Example 3:
Input: root = []
Output: []
Example 4:
Input: root = [1]
Output: [1]
Constraints:
[0, 100].-100 <= Node.val <= 100
Follow up: Recursive solution is trivial, could you do it iteratively?
Problem Overview: Given the root of a binary tree, return the preorder traversal of its node values. Preorder means you process nodes in the order root → left → right. The task checks your understanding of tree traversal and depth-first exploration patterns.
Approach 1: Recursive DFS Traversal (O(n) time, O(h) space)
The recursive approach follows the natural definition of preorder traversal: visit the current node, recursively traverse the left subtree, then recursively traverse the right subtree. You start at the root, append its value to the result list, and call the same function on node.left and node.right. Each node is processed exactly once, giving O(n) time complexity where n is the number of nodes. The recursion stack uses O(h) space, where h is the height of the tree (worst case O(n) for a skewed tree, O(log n) for a balanced tree). This approach maps directly to the definition of preorder and is usually the shortest and most readable implementation when recursion is allowed. It also reinforces the core idea of depth-first search in trees.
Approach 2: Iterative Traversal Using Stack (O(n) time, O(h) space)
The iterative solution simulates the recursion stack using an explicit stack. Start by pushing the root node onto the stack. While the stack is not empty, pop the top node, record its value, then push its right child followed by its left child. The push order matters: pushing the right child first ensures the left child is processed next, preserving preorder order (root → left → right). Each node enters and leaves the stack once, so the time complexity remains O(n). The stack holds at most the height of the tree at any time, giving O(h) auxiliary space. This approach is useful when recursion depth could cause stack overflow or when interviewers want to see how you convert recursive DFS logic into an iterative algorithm.
Recommended for interviews: Both approaches are acceptable, but interviewers often expect you to start with the recursive DFS because it directly reflects preorder traversal logic. After that, showing the iterative stack-based solution demonstrates deeper understanding of how recursion works under the hood. The recursive version proves you understand traversal order, while the stack-based version shows you can control the traversal manually using fundamental data structures.
The recursive approach naturally aligns with the definition of preorder traversal: visit the root first, then recursively traverse the left subtree, followed by the right subtree.
This C code defines a recursive function for the preorder traversal. It allocates space for the result and uses a helper function to populate the preorder values by traversing each node recursively.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N) where N is the number of nodes, as each node is visited once. Space Complexity: O(N) in the worst case due to recursion stack space.
The iterative approach replaces the recursive call stack with an explicit stack. Nodes are processed in preorder, using a stack to maintain traversal state.
In C, we use an array to simulate a stack, manually pushing and popping nodes while traversing the tree. Nodes are visited and added to the result list in prenode order.
C++
Java
Python
C#
JavaScript
Time Complexity: O(N), since each node is visited once. Space Complexity: O(N), for the stack used to store nodes.
| Approach | Complexity |
|---|---|
| Recursive Approach | Time Complexity: O(N) where N is the number of nodes, as each node is visited once. Space Complexity: O(N) in the worst case due to recursion stack space. |
| Iterative Approach using Stack | Time Complexity: O(N), since each node is visited once. Space Complexity: O(N), for the stack used to store nodes. |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive DFS Traversal | O(n) | O(h) | Cleanest implementation when recursion is acceptable and tree height is manageable |
| Iterative Traversal Using Stack | O(n) | O(h) | Preferred when avoiding recursion or when demonstrating explicit stack-based DFS |
Construct Binary Tree from Inorder and Preorder Traversal - Leetcode 105 - Python • NeetCode • 307,238 views views
Watch 9 more video solutions →Practice Binary Tree Preorder Traversal with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor