By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You need to traverse a tree structure and extract information from it—maybe find the maximum depth, collect values in a specific order, or process nodes level by level. Binary tree problems show up constantly in interviews, and there are two main approaches: depth-first search (DFS) and breadth-first search (BFS). Knowing when to use each one and how to implement them quickly will save you time under pressure.
A binary tree is a tree where each node has at most two children: a left child and a right child. This constraint makes them simpler to reason about than general trees, and it's the structure you'll encounter in most interview problems.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = rightEvery node stores a value and pointers to its left and right children. When either child doesn't exist, that pointer is None. This is the foundation for everything that follows.
DFS explores as far as possible along each branch before backtracking. The recursive approach is the most natural fit for trees because the recursive call stack naturally mirrors the tree structure.
You'll encounter three common DFS traversal orders: inorder (left, root, right), preorder (root, left, right), and postorder (left, right, root). The order matters depending on what you're trying to compute. For example, inorder traversal of a binary search tree yields values in sorted order.
# Inorder traversal: left → root → right
def inorder(root):
if not root: return []
return inorder(root.left) + [root.val] + inorder(root.right)
# Max depth
def max_depth(root):
if not root: return 0
return 1 + max(max_depth(root.left), max_depth(root.right))The inorder example shows the pattern: handle the base case (empty tree), then recurse on children and combine results. The max_depth example demonstrates postorder traversal—you need results from children before computing the parent's depth.
BFS processes nodes level by level, visiting all nodes at distance k before visiting nodes at distance k+1. Use a deque (double-ended queue) to efficiently add nodes at the end and remove them from the front.
The key trick is tracking how many nodes are in the current level. Before processing each level, record the queue length. That tells you exactly how many nodes to dequeue before moving to the next level.
from collections import deque
def level_order(root):
if not root: return []
result, queue = [], deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result.append(level)
return resultBFS shines when the problem explicitly asks for level-by-level output, or when you need to find the shortest path in an unweighted tree. It's also useful for problems where you need to process nodes in order of distance from the root.
DFS with recursion for most tree problems. BFS with a deque when you need level-by-level processing.
DFS is your default. It's easier to implement, uses less memory for wide trees, and handles most problems naturally. Reach for BFS when the problem explicitly mentions levels, or when you're looking for something like the shortest path or the leftmost node at each level.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.