How do I iterate over Binary Tree?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Iterating over a binary tree means visiting every node in a defined order. The four main traversal strategies are in-order (left, root, right), pre-order (root, left, right), post-order (left, right, root), and level-order (breadth-first). Each has distinct use cases — in-order produces sorted output from a BST, pre-order serializes the tree structure, post-order handles cleanup, and level-order processes nodes by depth. This article demonstrates both recursive and iterative implementations in Python.
Node Definition
In-Order Traversal (Left, Root, Right)
Recursive:
Iterative (using a stack):
In-order traversal of a BST yields values in sorted (ascending) order.
Pre-Order Traversal (Root, Left, Right)
Recursive:
Iterative:
Pre-order is used to serialize/copy a tree because the root is processed before its children.
Post-Order Traversal (Left, Right, Root)
Recursive:
Iterative:
Post-order is used for deleting trees (children are freed before the parent) and evaluating expression trees.
Level-Order Traversal (Breadth-First)
Level-order processes all nodes at depth d before any node at depth d+1. It uses a queue instead of a stack.
Traversal Comparison
| Traversal | Order | Data Structure | Use Case |
| In-order | Left, Root, Right | Stack | Sorted output from BST |
| Pre-order | Root, Left, Right | Stack | Serialize/copy tree |
| Post-order | Left, Right, Root | Stack | Delete tree, evaluate expressions |
| Level-order | By depth | Queue | Shortest path, level-by-level processing |
Common Pitfalls
- Using recursion on very deep trees: Python's default recursion limit is 1000. A skewed tree with 10,000 nodes causes
RecursionError. Use iterative traversal with an explicit stack for production code, or increase the limit withsys.setrecursionlimit(). - Pushing children in wrong order for pre-order iterative: In the stack-based pre-order, push the right child before the left. Since a stack is LIFO, the left child is popped first, producing the correct root-left-right order.
- Confusing in-order sorted output with any binary tree: In-order traversal only produces sorted output for a Binary Search Tree. For a general binary tree, in-order output has no guaranteed ordering.
- Modifying the tree during traversal: Inserting or deleting nodes while iterating can cause skipped nodes or infinite loops. Collect nodes into a list first, then modify the tree.
- Forgetting the base case in recursive traversal: Omitting
if node is None: returncausesAttributeErrorwhen accessing.leftor.rightonNone. Always check forNonebefore recursing.
Summary
- In-order (left, root, right) yields sorted values from a BST — use for ordered processing
- Pre-order (root, left, right) visits the root first — use for tree serialization and copying
- Post-order (left, right, root) visits the root last — use for deletion and expression evaluation
- Level-order (breadth-first) processes level by level using a queue
- Iterative traversals use an explicit stack (or queue) and avoid recursion depth limits
- All four traversals visit every node exactly once with O(n) time and O(h) or O(w) space
Related reading
- How do I iterate through the files in a directory and it's sub-directories in Java?
- How do I remove duplicates from a list, while preserving order?
- How do I reverse a list or loop over it backwards?
- How do I search for a number in a 2d array sorted left to right and top to bottom?
- How do I iterate through two lists in parallel?
- How do I iterate through two lists in parallel?
- How do I sort a dictionary by key?
- How do I sort a dictionary by key?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.