Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns

Topic · 4 patterns

Trees patterns

Tree problems are recursion with one question: what does each node return to its parent? BFS walks a tree level by level, DFS carries values down a path and answers back up, BST logic uses the ordering to discard half the tree, and construction and serialisation rebuild a tree from its traversals.

The patterns

  1. Tree BFS (Level Order)

    O(n)

    Process the tree level by level with a queue, recording the size of each level before draining it.

    Spot it: Anything per level: level order, averages, right-side view, zigzag, level width, or minimum depth / nearest leaf.

    Learn Tree BFSCheat sheet and practice
  2. Tree DFS (Paths & Recursion)

    O(n)

    Recurse down carrying state (running sum / depth); combine children's results on the way back up.

    Spot it: Root-to-leaf paths, path sums, LCA, or a value computed from children: height, diameter, balance, max path sum.

    Learn Tree DFSCheat sheet and practice
  3. BST Logic

    O(h)

    Use the ordering invariant: go left for smaller, right for larger; an inorder traversal yields sorted values.

    Spot it: The tree is a binary search tree: kth smallest, successor, range sum, closest value, validate or recover ordering, LCA by value.

    Learn first: Tree DFS (Paths & Recursion), Binary Search

    Learn BST LogicCheat sheet and practice
  4. Tree Construction & Serialization

    O(n)

    Preorder/postorder gives the root; inorder splits left vs right subtree. Recurse on the partitions.

    Spot it: Rebuild a tree from two traversals (preorder+inorder), serialize/deserialize to a string, or flatten/convert its structure.

    Learn first: Tree DFS (Paths & Recursion)

    Learn Tree Construction & SerializationCheat sheet and practice

Which one? Match the signal

If the problem saysReach for
"Level order", level-by-level, right-side view, zigzag, or minimum depthTree BFSO(n)
Root-to-leaf paths, path sums, or a recursive subtree property (height, diameter)Tree DFSO(n)
It says binary search tree; inorder is sorted; "kth smallest" / "validate"BST LogicO(h)
Rebuild a tree from traversals, or serialize / deserialize itTree ConstructionO(n)

Easy to confuse

  • Minimum Height Trees looks like Tree DFS (Paths & Recursion), but it’s Topological Sort

    Trying every root is O(n2); peeling leaves (Kahn on degree 1) finds centres.

Used together

  • Tree DFS (Paths & Recursion) + Prefix Sum + Hash Map / Hash Set Lookup

    When: count downward paths with a given sum. Downward paths starting anywhere are pairs of prefixes on the current root path.

  • Tree DFS (Paths & Recursion) + Graph BFS / DFS

    When: nodes at distance k from a target node; infection time from a node. Distances in a tree can go upward, which child pointers cannot express.

  • Binary Lifting & LCA + Tree DFS (Paths & Recursion)

    When: distance between two nodes, k-th ancestor, many queries. Many LCA/distance queries make linear walks too slow.

  • Small-to-Large Merging (DSU on tree) + Tree DFS (Paths & Recursion)

    When: distinct colours per subtree; dominant colour sums. Building a set per subtree naively is O(n2).

Where to start

then the The 128 and The 365 sheets

Tree BFS (Level Order)

  • Binary Tree Level Order TraversalMedium
  • Average of Levels in Binary TreeEasy

Tree DFS (Paths & Recursion)

  • Maximum Depth of Binary TreeEasy
  • Same TreeEasy
  • Path SumEasy

BST Logic

  • Insert into a Binary Search TreeMedium
  • Range Sum of BSTEasy

Tree Construction & Serialization

  • Convert Sorted Array to Binary Search TreeEasy
  • Construct Binary Tree from Preorder and Inorder TraversalMedium

Further reading

free, outside Lessgo
  • Binary Tree explore cardLeetCode

    Traversals, top-down vs bottom-up recursion; core interview patterns.

  • Tree cheatsheetTech Interview Handbook

    BST properties, traversals and serialization problem list.

  • BST visualizerVisuAlgo

    Animate BST/AVL operations and traversals.

  • Introduction to Tree AlgorithmsUSACO Guide

    Trees as graphs: DFS on trees, subtree sizes, diameter.

  • Lowest Common Ancestorcp-algorithms

    LCA techniques beyond the interview version; binary lifting.

  • NeetCode RoadmapNeetCode

    Pattern-ordered problem tree with a short video per problem; best interview-first path.

← Backtracking & RecursionAll 49 patternsGraphs →
TodayReviewPatternsProblems