What is the time and space complexity of a breadth first and depth first tree traversal?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Breadth-first search (BFS) and depth-first search (DFS) are two fundamental algorithms used to traverse or search through trees and graphs. Understanding these traversals' time and space complexities is crucial in computational fields such as data structure optimization, algorithm analysis, and artificial intelligence.
Breadth-First Search (BFS)
BFS traverses a tree (or graph) level by level, visiting all nodes at the current level before proceeding to the next level. This process uses a queue to track the nodes at the current level.
Time Complexity
The time complexity of BFS is generally considered , where is the number of vertices (or nodes) and is the number of edges in the graph. This is because each node and each edge is processed once:
- The iteration over all vertices requires time.
- Processing all edges requires time.
In the case of a tree specifically, which is a special type of graph with edges, the time complexity becomes linear, i.e., .
Space Complexity
The space complexity of BFS primarily depends on the storage required for the queue:
- In the worst case, the queue can store all nodes at the bottom level, which is the maximum number of nodes in a level for a well-balanced tree. This number is approximately equal to , where is the depth of the tree.
- Therefore, the space complexity is , where is the branching factor (or the maximum number of children a node can have).
BFS tends to require more space than DFS due to its need to keep track of all nodes at the current level.
Depth-First Search (DFS)
Unlike BFS, DFS explores as far down a branch as possible before backtracking. It typically uses a stack, either explicitly through an iterative method or implicitly via recursion.
Time Complexity
The time complexity for DFS, similar to BFS, is for graphs:
- Each vertex is processed once.
- Each edge is checked once to ensure no cycles are revisited.
Again, for tree structures, the time complexity simplifies to since there are edges.
Space Complexity
DFS's space complexity significantly benefits from its backtracking nature:
- When implemented recursively, the space complexity is due to the recursion stack, where is the depth of the tree.
- For an iterative approach using an explicit stack, the space complexity also remains . In a worst-case linear tree (essentially a linked list), the space requirement is capped at .
DFS is often preferred in memory-constrained environments due to its lower memory overhead compared to BFS.
Summary Table
Below is a table summarizing the time and space complexity of BFS and DFS:
| Algorithm | Time Complexity | Space Complexity |
| BFS | where is the branching factor and is the depth of the tree | |
| DFS | , where is the depth of the tree (or in the worst case of a linear tree) |
Additional Considerations
- Memory Usage: While DFS generally uses less memory than BFS, be wary of stack overflow issues in languages with limited recursion depth.
- Path Finding: BFS tends to find the shortest path in an unweighted graph or tree, while DFS does not guarantee this.
- Iterative Deepening DFS (IDDFS): This algorithm combines depth-first search’s memory efficiency and breadth-first search’s completeness into a hybrid approach. It involves repeated application of DFS to increasing depths, providing the benefits of both BFS and DFS.
Understanding the nuances of BFS and DFS complexity is pivotal in selecting the appropriate traversal method for various computational problems and optimizing algorithm performance.
Related reading
- What is the time complexity of heapq.nlargest?
- What is the time complexity of java.util.HashMap class' keySet method?
- What is the time complexity of k-means?
- What is the time complexity of my function?
- What is the time complexity of popping an element from a dict in Python?
- What is the time complexity of traversing a 2d array
- What is the time complexity big O of sortedArrayUsingComparator? iOS/OSX
- What is the time complexity of Ruby's built in permutation and repeated_permutation methods?

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.