SLIDE 1: Section Intro, Tree Patterns

Section 1: Tree Patterns

Trees are one of the most heavily tested data structures in coding interviews. A tree is a linked list that branches: instead of one .next pointer, each node has .left and .right. This branching structure means that iteration alone is not enough. You need recursion (or an explicit stack/queue) to traverse the tree, and you need to reason about what information flows down from parent to child and what information returns upward from child to parent.

Most tree problems reduce to a single question: should I use DFS (go deep first) or BFS (go level by level)? And for DFS: what do I pass down, and what do I return up? Once you can answer these two questions for any tree problem, the code follows naturally.

What We’ll Cover:

  1. Tree terminology and mental model
  2. Recursive DFS: preorder, inorder, postorder
  3. Returning values upward in recursion
  4. Iterative DFS with explicit stack
  5. BFS and level order traversal
  6. BST invariants and validation
  7. Tree path problems and lowest common ancestor

Where This Appears in Interviews:

Company Type How This Section Appears
FAANG / Big Tech Diameter of Binary Tree, Validate BST, and Lowest Common Ancestor are top-20 most-asked
Mid-size companies Maximum Depth, Level Order Traversal, and Path Sum are common phone screen questions
Startups Tree problems are less frequent but appear when testing recursive thinking
Online assessments Tree traversal and BST problems are common timed questions

Core Idea for This Section: Most tree problems are DFS or BFS plus a decision about what information flows down (parameters) and what information returns upward (return values).


SLIDE 2: Tree Terminology and Mental Model

Key Terms:

Term Definition
Root The topmost node, has no parent
Leaf A node with no children (both left and right are null)
Edge A connection between a parent and a child
Depth Distance from the root to a node (root has depth 0)
Height Distance from a node to its deepest leaf descendant (leaf has height 0)
Subtree A node plus all of its descendants
Binary tree Each node has at most two children
Binary search tree (BST) Left subtree values < node value < right subtree values
Balanced tree Height difference between left and right subtrees is at most 1
Complete tree Every level is full except possibly the last, which fills left to right