LeetCode 144: Binary Tree Preorder Traversal – Java Recursive & Iterative Solution Explained

Learn how to solve LeetCode 144 Binary Tree Preorder Traversal using Recursive and Iterative approaches in Java. Includes traversal intuition, stack-based solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
2 views
LinkedInGithubX
0
0
LeetCode 144: Binary Tree Preorder Traversal – Java Recursive & Iterative Solution Explained
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 144 – Binary Tree Preorder Traversal is one of the most important beginner-friendly tree traversal problems in Data Structures and Algorithms.

This problem helps you understand:

  1. Binary Tree Traversal
  2. Depth First Search (DFS)
  3. Recursion
  4. Stack-based traversal
  5. Tree traversal patterns

Preorder traversal is widely used in:

  1. Tree copying
  2. Serialization
  3. Expression trees
  4. DFS-based problems
  5. Hierarchical data processing

It is also one of the most commonly asked tree problems in coding interviews.

Problem Link

🔗 Problem

LeetCode 144: Binary Tree Preorder Traversal

Official Problem:

LeetCode Problem Link

Problem Statement

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

What is Preorder Traversal?

In preorder traversal, nodes are visited in this order:

Root → Left → Right

The root node is processed first before traversing subtrees.

Example

Input

root = [1,null,2,3]

Tree Structure:

1
\
2
/
3

Preorder Traversal

Traversal order:

1 → 2 → 3

Output:

[1,2,3]

Recursive Approach (Most Common)

Intuition

In preorder traversal:

  1. Visit current node
  2. Traverse left subtree
  3. Traverse right subtree

This naturally fits recursion because trees themselves are recursive structures.

Recursive DFS Visualization

Traversal pattern:

Root → Left → Right

Recursive function:

visit(node)
preorder(node.left)
preorder(node.right)

Java Recursive Solution

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* }
*/

class Solution {

public void solve(List<Integer> list, TreeNode root) {

if(root == null) return;

list.add(root.val);

solve(list, root.left);

solve(list, root.right);
}

public List<Integer> preorderTraversal(TreeNode root) {

List<Integer> list = new ArrayList<>();

solve(list, root);

return list;
}
}

Dry Run – Recursive Approach

Tree:

1
\
2
/
3

Step 1

Start at:

1

Add:

1

Move right to:

2

Step 2

Add:

2

Move left to:

3

Step 3

Add:

3

Final Answer

[1,2,3]

Time Complexity – Recursive

Time Complexity

O(N)

Every node is visited once.

Space Complexity

O(H)

Where:

  1. H = height of tree
  2. Recursive call stack uses extra space

Worst case:

O(N)

for skewed trees.

Iterative Approach (Interview Follow-Up)

The follow-up asks:

Can you solve it iteratively?

Yes.

We use a stack to simulate recursion.

Iterative Preorder Intuition

Preorder traversal order is:

Root → Left → Right

Using a stack:

  1. Process current node immediately
  2. Push right child first
  3. Push left child second

Why?

Because stacks follow:

Last In First Out (LIFO)

So left subtree gets processed first.

Stack-Based Iterative Logic

Algorithm

  1. Push root into stack.
  2. Pop node.
  3. Add node value.
  4. Push right child.
  5. Push left child.
  6. Repeat until stack becomes empty.

Java Iterative Solution

class Solution {

public List<Integer> preorderTraversal(TreeNode root) {

List<Integer> ans = new ArrayList<>();

if(root == null) return ans;

Stack<TreeNode> stack = new Stack<>();

stack.push(root);

while(!stack.isEmpty()) {

TreeNode node = stack.pop();

ans.add(node.val);

if(node.right != null) {
stack.push(node.right);
}

if(node.left != null) {
stack.push(node.left);
}
}

return ans;
}
}

Dry Run – Iterative Approach

Tree:

1
\
2
/
3

Step 1

Push:

1

Step 2

Pop:

1

Add:

[1]

Push right child:

2

Step 3

Pop:

2

Add:

[1,2]

Push left child:

3

Step 4

Pop:

3

Add:

[1,2,3]

Final Answer

[1,2,3]

Comparison of Approaches

ApproachAdvantagesDisadvantages
RecursiveEasy to understandUses recursion stack
IterativeBetter interview practiceSlightly harder logic

Interview Explanation

In interviews, explain:

Preorder traversal processes nodes in Root → Left → Right order. Recursion naturally handles this traversal. Iteratively, we use a stack and push the right child before the left child so the left subtree gets processed first.

This demonstrates strong DFS and stack understanding.

Common Mistakes

1. Wrong Traversal Order

Incorrect:

Left → Root → Right

That is inorder traversal.

Correct preorder:

Root → Left → Right

2. Forgetting Null Base Case

Always check:

if(root == null) return;

3. Wrong Stack Push Order

For iterative traversal:

  1. Push right first
  2. Push left second

Otherwise traversal order becomes incorrect.

FAQs

Q1. Why is preorder traversal useful?

It is heavily used in:

  1. Tree cloning
  2. Serialization
  3. DFS traversal
  4. Expression trees

Q2. Which approach is preferred in interviews?

Recursive is simpler.

Iterative is often asked as a follow-up.

Q3. Can preorder traversal be done without stack or recursion?

Yes.

Using Morris Traversal.

Q4. What is the difference between preorder, inorder, and postorder?

TraversalOrder
PreorderRoot → Left → Right
InorderLeft → Root → Right
PostorderLeft → Right → Root

Bonus: Morris Preorder Traversal

Morris traversal performs preorder traversal using:

O(1)

extra space.

This is considered an advanced interview topic.

Conclusion

LeetCode 144 is one of the most fundamental binary tree traversal problems.

It teaches:

  1. DFS traversal
  2. Recursion
  3. Stack simulation
  4. Binary tree fundamentals

The key preorder pattern is:

Root → Left → Right

Mastering this traversal builds a strong foundation for advanced tree problems such as:

  1. Tree serialization
  2. DFS-based problems
  3. Tree reconstruction
  4. Expression trees
  5. Morris traversal
Ai Assistant Kas