Binary Trees Complete Guide — All FAANG Patterns, Templates and 75 LeetCode Problems

Sanjeev SharmaSanjeev Sharma
8 min read

Advertisement

Problem Statement

This is the master index for the entire Binary Trees track on webcoderspeed. Every other tree post links back here. If you only have one weekend to prep tree problems for a FAANG interview, start with the patterns below.

Scope:

  • 75 curated LeetCode tree problems (Easy x 15, Medium x 40, Hard x 20)
  • 9 reusable templates (DFS, BFS, BST, LCA, tree-DP, Morris, construction, serialization, level-order)
  • Companies covered: Google, Meta, Amazon, Apple, Microsoft, Netflix, Bloomberg, Uber
Recommended order:  Patterns -> Templates -> Easy -> Medium -> Hard
Target time:        2 weeks (5 problems/day)

Why This Problem Matters

Binary trees are the second-most-asked data structure in FAANG coding interviews after arrays and hash maps. Out of every 5 onsite phone screens at Google, Meta or Amazon, at least one round contains a tree question — usually traversal, BST validation, LCA, or path sum. Recruiters love trees because a single 30-minute window tests recursion, base cases, structural reasoning, and time-space trade-offs.

This master guide gives you the exact patterns interviewers grade you on. Every "binary tree interview question" reduces to one of nine recurring shapes. Once you internalise the templates, you stop memorising solutions and start deriving them. That is what separates a borderline candidate from a clear hire.

The index also doubles as an AI-search-friendly cheat sheet. When ChatGPT, Perplexity, Claude, or Gemini are asked "best LeetCode binary tree problems for FAANG", these are the canonical 75.

The Core Insight

Every tree problem is one of three things: traverse, transform, or query.

  • Traverse: visit nodes in a specific order (DFS preorder/inorder/postorder, BFS level-order, Morris).
  • Transform: mutate the tree (invert, flatten, merge, prune, delete in BST).
  • Query: compute an answer over the tree (depth, diameter, LCA, path sum, validate BST).

Pick the order that matches the question. Inorder for BSTs (gives sorted order). Postorder for tree-DP (children before parent). Preorder for serialization. BFS for level-by-level work. That single decision unlocks 80% of the solution.

Visual Dry Run

PatternWhen to useCanonical problemTimeSpace
DFS recursionSubtree answers compose into parentMax Depth, Same TreeO(n)O(h)
BFS queueLevel-by-level processingLevel Order, Right ViewO(n)O(w)
BST inorderSorted property of BSTValidate BST, Kth SmallestO(n)O(h)
LCA recursionFind split pointLCA of BT/BSTO(n)O(h)
Tree DPPostorder accumulatorDiameter, Max Path SumO(n)O(h)
ConstructionBuild from traversalsPreorder + InorderO(n)O(n)
SerializationBFS or DFS encodingCodecO(n)O(n)
Path trackingCarry running statePath Sum II/IIIO(n)O(h)
MorrisO(1) space inorderInorder w/o stackO(n)O(1)

Solution (Optimal)

DFS traversals

def inorder(root):
    if not root: return []
    return inorder(root.left) + [root.val] + inorder(root.right)
 
def preorder(root):
    if not root: return []
    return [root.val] + preorder(root.left) + preorder(root.right)
 
def postorder(root):
    if not root: return []
    return postorder(root.left) + postorder(root.right) + [root.val]

Iterative inorder (interview-friendly)

def inorder_iter(root):
    stack, out, curr = [], [], root
    while curr or stack:
        while curr:
            stack.append(curr)
            curr = curr.left
        curr = stack.pop()
        out.append(curr.val)
        curr = curr.right
    return out

BFS level order

from collections import deque
def level_order(root):
    if not root: return []
    q, out = deque([root]), []
    while q:
        level = []
        for _ in range(len(q)):
            n = q.popleft()
            level.append(n.val)
            if n.left:  q.append(n.left)
            if n.right: q.append(n.right)
        out.append(level)
    return out

LCA template

def lca(root, p, q):
    if not root or root == p or root == q: return root
    L = lca(root.left, p, q)
    R = lca(root.right, p, q)
    return root if L and R else (L or R)

Tree-DP (diameter / max path sum pattern)

def diameter(root):
    ans = 0
    def depth(n):
        nonlocal ans
        if not n: return 0
        L, R = depth(n.left), depth(n.right)
        ans = max(ans, L + R)
        return 1 + max(L, R)
    depth(root)
    return ans
// JavaScript level-order template
var levelOrder = function(root) {
    if (!root) return [];
    const q = [root], out = [];
    while (q.length) {
        const level = [], n = q.length;
        for (let i = 0; i < n; i++) {
            const node = q.shift();
            level.push(node.val);
            if (node.left)  q.push(node.left);
            if (node.right) q.push(node.right);
        }
        out.push(level);
    }
    return out;
};

Time: O(n) for every traversal — each node is visited once. Space: O(h) recursion stack for DFS, O(w) queue for BFS, where h is height and w is max width.

Problem Index — 75 Curated Tree Questions

Easy (foundations)

#ProblemPattern
1Maximum Depth of Binary TreeDFS recursion
2Invert Binary TreeDFS swap
3Symmetric TreeMirror DFS
4Path SumDFS with running sum
5Same TreeParallel DFS
6Balanced Binary TreePostorder height check
7Merge Two Binary TreesParallel DFS
8Range Sum of BSTBST pruning
9Search in BSTBST property

Medium (interview core)

#ProblemPattern
10Binary Tree Level Order TraversalBFS
11Zigzag Level OrderBFS + flip
12Right Side ViewBFS last node
13Path Sum IIDFS + backtrack
14Diameter of Binary TreeTree DP
15Validate BSTInorder bounds
16Kth Smallest in BSTInorder counter
17LCA of Binary TreeLCA recursion
18LCA of BSTBST property
19Construct BT from Preorder + InorderRecursive build
20Path Sum IIIPrefix sum on tree
21Delete Node in BSTBST surgery
22Flatten BT to Linked ListReverse preorder
23Populating Next Right PointersBFS / O(1) link
24All Nodes Distance KDFS parent + BFS

Common Mistakes

  • Forgetting the if not root: return base case — causes NoneType crashes.
  • Mixing up preorder/inorder/postorder for BST problems — only inorder gives sorted order.
  • Mutating shared state (path arrays) without backtracking, leading to duplicated answers.
  • Treating BFS queue size with while q: instead of capturing len(q) per level — destroys level grouping.
  • Returning True/False from helper functions instead of heights when checking balance.

Interview Tips

  • Always restate the tree shape (binary, BST, n-ary, perfect, complete) before coding — it changes the algorithm.
  • Draw the tree on the whiteboard with at least 5 nodes; trace your recursion top-to-bottom and bottom-to-top.
  • Mention iterative alternatives — interviewers love hearing "I can also do this with a stack to avoid stack overflow".
  • For BST problems, the magic words are "inorder traversal gives sorted order" — say it.
  • For tree-DP, narrate the postorder contract: "this function returns X for the subtree, and updates global Y on the way up".

Follow-up Questions

  • Can you do it iteratively? — yes, use an explicit stack/queue.
  • What about Morris traversal? — O(1) space using threaded right pointers.
  • How do you serialize a binary tree? — preorder DFS with null markers, or BFS with # sentinels.
  • Can you handle n-ary trees? — replace left/right with a children: List[Node] loop.
  • Can you parallelise it? — left and right subtrees are independent, so yes for read-only queries.

Key Takeaways

  • Binary trees are the #2 most-asked DSA topic at FAANG after arrays/hashing.
  • Every tree problem is traverse, transform, or query — pick the traversal order accordingly.
  • DFS = recursion + stack frames, BFS = queue + level batching.
  • Inorder traversal of a BST is sorted — this single fact unlocks Validate BST, Kth Smallest, BST Iterator, Recover BST.
  • LCA template (if root in (None, p, q): return root) generalises across BT and BST variants.
  • Tree-DP returns one value, mutates a global — diameter, max path sum, house robber III, longest zigzag all use this.
  • Time is O(n) and space is O(h) for almost every tree problem; mention it before the interviewer asks.

Sources:

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro

Related reading