Is the runtime of BFS and DFS on a binary tree ON?
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
For a binary tree with N nodes, both Breadth-First Search and Depth-First Search run in linear time, written as O(N). The confusion usually comes from mixing runtime with memory complexity, or from applying graph formulas without simplifying for tree structure. The short answer is yes, both traversals are O(N) in time, but they differ in space behavior.
Why Both Traversals Are Linear Time
A full traversal must visit each node at least once, so O(N) is a lower bound. BFS and DFS both reach that lower bound because each node is processed a constant number of times.
BFS work per node:
- dequeue once
- inspect up to two children
- enqueue existing children once
DFS work per node:
- pop once from stack or enter once in recursion
- inspect up to two children
- push or recurse to each child once
Since per-node work is constant and repeated for all nodes, total work scales linearly.
BFS Example with Complexity
BFS explores level by level using a queue.
Runtime is O(N) because each node enters and leaves the queue once. Space is O(W), where W is maximum tree width. In a complete tree, width can be proportional to N, so worst-case auxiliary space is O(N).
DFS Example with Complexity
DFS explores one branch deeply before backtracking.
Runtime is again O(N) because every node is processed once. Space is O(H), where H is tree height. Balanced trees have height near log N, while skewed trees can have height near N.
Relation to Graph Formula O(V + E)
You may also see traversal complexity expressed as O(V + E) for graphs. That is correct in general. For trees:
- vertices
V = N - edges
E = N - 1
So O(V + E) becomes O(N + N - 1), which simplifies to O(N). This is why both formulations agree.
Balanced Versus Skewed Trees
Time complexity does not change with shape for full traversal. Space can change a lot.
- BFS memory grows with width.
- DFS memory grows with height.
On wide balanced trees, DFS often uses less memory than BFS. On very deep skewed trees, recursive DFS can hit recursion limits, so iterative DFS is safer.
Runnable Example Comparison
Use one test tree and compare outputs.
Both functions visit all six nodes once, which matches linear runtime behavior.
Choosing Between BFS and DFS in Practice
Since time complexity is the same for full-tree traversal, choose based on problem requirements.
Use BFS when:
- you need level-order results
- you need nearest match by edge distance
- you need shortest path in an unweighted tree
Use DFS when:
- you need root-to-leaf path exploration
- backtracking logic is natural
- memory pressure from wide levels is a concern
The algorithm choice is often about traversal order and memory profile, not runtime class.
Common Pitfalls
A common pitfall is saying DFS is O(log N) because balanced tree height is log N. That is space intuition, not full traversal runtime. Another mistake is comparing BFS and DFS only on time while ignoring queue and stack growth. Developers also apply tree assumptions directly to arbitrary graphs and forget visited sets, which can lead to repeated processing or infinite loops. Finally, recursive DFS on very deep trees can fail due to recursion depth limits even though asymptotic runtime is still linear.
Summary
- Yes, BFS and DFS on a binary tree both run in
O(N)time. - Both traversals process each node a constant number of times.
- BFS uses
O(W)space, DFS usesO(H)space. - Graph formula
O(V + E)simplifies toO(N)for trees. - Choose BFS or DFS based on traversal goals and memory constraints, not runtime class.
Related reading
- Is the sorting algorithm used by .NET's Array.Sort method a stable algorithm?
- Is the time-complexity of iterative string append actually On2, or On?
- Is the time complexity of the empty algorithm O0?
- Is there a better way to guess possible unknown variables without brute force than I am doing? Machine learning?
- Is there a bug in java.util.Stack's Iterator?
- Is there a built-in Binary Search Tree in .NET 4.0?
- Is the Scala 2.8 collections library a case of the longest suicide note in history?
- Is there a better way to guess possible unknown variables without brute force than I am doing? Machine learning?

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.