Why This Still Matters in 2026
Every coding interview prep guide will tell you: "Just use recursion for trees, it's cleaner." Then you hit a binary tree with 50,000 nodes in production, watch Python throw RecursionError: maximum recursion depth exceeded, and realize clean code that crashes isn't very clean.
I've seen both approaches in real codebases. The recursive DFS that worked perfectly in local tests but died in prod when a user uploaded a pathologically deep tree. The iterative BFS that looked ugly but scaled without thinking. The question isn't which is "better" — it's when each approach actually makes sense.
Let's build both, break both, and figure out what you'd actually write when the interviewer isn't watching.
The Recursive Approach: Start Here
Recursion mirrors the tree structure perfectly. Each node handles itself, then delegates to its children. Here's inorder traversal (left → root → right) in its purest form:
python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorder_recursive(root):
"""Returns list of node values in inorder sequence."""
result = []
def traverse(node):
if not node:
return
traverse(node.left)
result.append(node.val)
traverse(node.right)
traverse(root)
return result
# Test it
---
*Continue reading the full article on [TildAlice](https://tildalice.io/stack-vs-recursion-tree-traversal-clean-code/)*
Top comments (0)