Skip to main content

Binary Tree Preorder Traversal - Solution & Explanation

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

Problem Statement

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:

  • 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 preorder traversal of its nodes' values. Preorder traversal processes nodes in the order root → left → right, meaning you visit the current node first, then recursively explore the left subtree followed by the right subtree.

Approach 1: Recursive DFS (Time: O(n), Space: O(h))

This approach directly mirrors the definition of preorder traversal. Start from the root, append its value to the result, recursively traverse the left child, then recursively traverse the right child. The recursion implicitly uses the call stack to track traversal state. The algorithm visits each node exactly once, so the time complexity is O(n), where n is the number of nodes. Space complexity is O(h), where h is the height of the tree due to the recursion stack (worst case O(n) for a skewed tree, O(log n) for balanced trees). This method is concise and ideal when recursion depth is manageable. It relies on Depth-First Search concepts applied to a Binary Tree.

Approach 2: Iterative Traversal Using Stack (Time: O(n), Space: O(h))

The iterative solution simulates the recursion stack using an explicit stack. Push 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. Pushing the right child first ensures the left child is processed next, preserving preorder order (root → left → right). Each node is pushed and popped at most once, resulting in O(n) time complexity. The stack holds at most O(h) nodes at a time, where h is the tree height. This approach avoids recursion and works well in environments where recursion depth may cause stack overflow.

Recommended for interviews: Interviewers typically expect the recursive DFS solution first because it demonstrates understanding of preorder traversal and tree recursion. The iterative stack approach shows deeper knowledge of how recursion works internally and proves you can convert recursive tree algorithms into iterative ones. Showing both solutions signals strong mastery of tree traversal patterns.

Approach 1: Recursive Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Iterative Approach using Stack

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(N), since each node is visited once. Space Complexity: O(N), for the stack used to store nodes.

Try this approach in the editor →

Approach 3: Recursive Traversal

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

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 Non-Recursive Traversal

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 right child of the node into the stack, then push the left child of the node into the stack (if there are child nodes).
  5. Repeat steps 2-4.
  6. Return the 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 Preorder 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 left 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.right.
  2. If the left subtree of the current node root is not empty, find the rightmost node pre of the left subtree (which is the predecessor of the root node in inorder traversal):
    • If the right subtree of the predecessor node pre is empty, add the current node value to the result list ans, then point the right subtree of the predecessor node to the current node root, and update the current node to root.left.
    • If the right subtree of the predecessor node pre is not empty, point the right subtree of the predecessor node to null (i.e., disconnect pre and root), and update the current node to root.right.
  3. Repeat the above steps until the binary tree node is null, and the traversal ends.

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 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.

Recursive Traversal
Stack Implementation for Non-Recursive Traversal
Morris Preorder Traversal

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recursive DFSO(n)O(h)Best for clarity and when recursion depth is safe. Mirrors preorder definition.
Iterative Using StackO(n)O(h)Preferred when avoiding recursion or when explicit control of traversal stack is needed.

Video Solution

Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - PythonNeetCodeIO38,612 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Binary Tree Preorder Traversal easy or hard?
Binary Tree Preorder Traversal is classified as an easy problem because the algorithm directly follows the preorder definition. The main challenge is understanding tree traversal order and translating it into recursive or stack-based code.
Binary Tree Preorder Traversal Python/Java solution
Python and Java solutions typically implement either recursive DFS or an iterative stack approach. Both versions run in O(n) time and O(h) space. The recursive solution is shorter, while the iterative version gives explicit control over the traversal stack.
How to solve Binary Tree Preorder Traversal in O(n)?
Traverse the tree using Depth-First Search and process nodes in root → left → right order. Append the node value when first visiting the node, then explore its left subtree followed by the right subtree. Each node is processed once, resulting in O(n) time complexity.
What is the best approach for Binary Tree Preorder Traversal?
Depth-First Search using recursion is the most common approach because preorder traversal naturally follows the pattern root → left → right. The recursive solution visits each node once with O(n) time and O(h) space, where h is the tree height. An iterative stack-based solution is equally optimal and often preferred when avoiding recursion.
Is Binary Tree Preorder Traversal asked at Google/Amazon/Meta?
Binary tree traversal problems frequently appear in interviews at companies like Amazon, Google, Meta, and Microsoft. While the exact problem may vary, understanding preorder, inorder, and postorder traversal is considered a fundamental tree concept tested in coding interviews.
What data structure is used in Binary Tree Preorder Traversal?
The recursive solution relies on the call stack to manage traversal state. The iterative approach explicitly uses a stack data structure to simulate recursion and ensure nodes are processed in root → left → right order.
What is the time complexity of Binary Tree Preorder Traversal?
Binary Tree Preorder Traversal runs in O(n) time because every node in the tree is visited exactly once. Both recursive DFS and iterative stack implementations achieve this complexity. Space complexity is O(h), where h represents the height of the tree due to recursion or stack storage.

Ready to solve this problem?

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

Practice on FleetCode