Skip to main content
TreesBFSDFSLeetCode

Binary Tree LeetCode Problems — Every Pattern You Need [2026]

· 9 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Trees appear in nearly every FAANG interview. Here's every tree pattern — DFS, BFS, LCA, BST, path problems — with the exact problems that test each one.

Why trees are everywhere in interviews

Trees appear in roughly 25% of FAANG interview problems. They test recursion naturally, have multiple traversal orders (pre/in/post/level), and combine well with other patterns (DP, BFS, hash maps). The good news: most tree problems are variations of 5–6 core patterns. Master those patterns and you can handle any tree problem.

Pattern 1: DFS traversal

The foundation. Preorder (root → left → right) for serialization. Inorder (left → root → right) gives sorted order in BSTs. Postorder (left → right → root) when you need children's results before processing the parent — height, diameter, balanced check. Key problems: Maximum Depth (LC 104), Invert Binary Tree (LC 226), Path Sum (LC 112), Diameter of Binary Tree (LC 543), Balanced Binary Tree (LC 110).

Pattern 2: BFS / level order

Use a queue. Process nodes level by level. Essential for: shortest path in a tree, level averages, right-side view, zigzag traversal. The template: push root, while queue not empty → process all nodes at current level, push their children. Key problems: Level Order Traversal (LC 102), Right Side View (LC 199), Average of Levels (LC 637), Maximum Width (LC 662).

Pattern 3: Path problems

These are the trickiest. The key insight: in a tree, a path can go through any node. A recursive function typically returns the best path through the current node upward (to parent), but also considers the path that passes through the current node (left branch + node + right branch) as a potential global answer. Key problems: Binary Tree Maximum Path Sum (LC 124) — most asked at FAANG. Path Sum II (LC 113). Sum Root to Leaf Numbers (LC 129).

Pattern 4: BST operations

BSTs give O(log n) search, insert, delete by maintaining the invariant: left < node < right. In-order traversal always gives sorted order — use this to validate BSTs or find kth smallest. Key problems: Validate BST (LC 98), Kth Smallest in BST (LC 230), Lowest Common Ancestor in BST (LC 235), Insert into BST (LC 701), Delete Node in BST (LC 450).

Pattern 5: LCA and ancestor problems

Lowest Common Ancestor is a flagship problem. For BSTs: if both targets are less than root, go left; if both greater, go right; otherwise root is LCA. For general trees: DFS — if you find p in the left subtree and q in the right subtree, current node is LCA. Key problems: LCA of Binary Tree (LC 236), LCA of BST (LC 235), Kth Ancestor of a Node (LC 1483).

Frequently Asked Questions

What tree problems appear most in FAANG interviews?
The most common tree problems at top companies: Maximum Depth of Binary Tree (LC 104), Invert Binary Tree (LC 226), Binary Tree Maximum Path Sum (LC 124 — hard, very common at FAANG), Lowest Common Ancestor (LC 236), Serialize/Deserialize Binary Tree (LC 297), Level Order Traversal (LC 102), and Validate BST (LC 98). Binary Tree Maximum Path Sum appears in roughly 30% of reported FAANG tree rounds.
What is the difference between pre-order, in-order, and post-order traversal?
Pre-order (root → left → right): used for serialization, copying a tree. In-order (left → root → right): gives sorted output for BSTs; used to find kth smallest. Post-order (left → right → root): used when you need children's results before processing the parent — tree height, diameter, balanced check. Level-order (BFS by row): shortest path, level averages, right-side view.
How do you find the Lowest Common Ancestor of a binary tree?
For a general binary tree: DFS from the root. If you find p in the left subtree and q in the right subtree, the current node is their LCA. If both are in the left subtree, recurse left. If both are in the right subtree, recurse right. For a BST, it is simpler: if both targets are less than root, go left; if both greater, go right; otherwise root is LCA.
When should I use BFS vs DFS for tree problems?
Use BFS (level-order) when the problem involves levels, minimum depth, right-side view, or any question where you need to process the tree layer by layer. Use DFS when you need to explore paths, compute subtree properties (height, diameter), detect cycles, or traverse in a specific order. DFS is easier to implement recursively. BFS requires an explicit queue but never hits recursion depth limits.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee