6 patterns · 28 problems
Binary Trees & BST
Traverse, build, compare, and validate binary trees and binary search trees.
Each pattern below starts with when to reach for it and the clue in the prompt that gives it away. Then it runs on real problems: pick one to watch the technique step by step, open its full solution, or practise it on an original problem with graded tests.
01Pattern
Traversal
Visit every node in a defined order
O(n)When to reach for it
- You need to visit each node in the tree exactly once, exploring all nodes in a specific order (e.g., inorder, preorder, postorder).
Spot it in the prompt
Look for problems where you must visit each node systematically without missing any, often requiring recursion or iteration.
Binary Tree Inorder Traversal
How to solve it
Animation shows the Traversal techniqueRecurse left, visit the node, then recurse right; iteratively simulate this with a stack pushing left children first.
Manifold Valve Readout is our own problem on the same technique: a new statement, graded in your browser.
02Pattern
Construction
Rebuild trees from traversal sequences
When to reach for it
- You need to create a binary tree from given input data, such as an array, inorder and preorder/postorder traversal sequences, or level order traversal.
Spot it in the prompt
Explore problems where you must build a binary tree based on provided specifications, reconstructing it from its serialized form or given traversal sequences.
Construct Binary Tree from Preorder and Inorder Traversal
How to solve it
Animation shows the Construction techniqueTake the preorder head as root, locate it in inorder to split left and right subtrees, and recurse using an index map.
Cave Survey Rebuild is our own problem on the same technique: a new statement, graded in your browser.
03Pattern
Mirror & Symmetry
Compare or flip left and right subtrees
When to reach for it
- You are tasked with determining whether a binary tree is symmetric or transforming it into its mirror image.
Spot it in the prompt
Look for problems where you compare the left and right subtrees recursively or swap the left and right children to achieve symmetry.
Invert Binary Tree
How to solve it
Animation shows the Mirror & Symmetry techniqueRecursively swap left and right children at every node, or do it iteratively with a queue or stack.
Mirror The Splitter Harness is our own problem on the same technique: a new statement, graded in your browser.
04Pattern
Path Sum & Root to Leaf
Track running sums along root-to-leaf paths
When to reach for it
- You need to find paths in a binary tree that sum up to a target value or enumerate all root-to-leaf paths.
Spot it in the prompt
Explore problems where you traverse the tree while keeping track of the path and its sum, recursively exploring all possible paths.
Path Sum
How to solve it
Animation shows the Path Sum & Root to Leaf techniqueDFS down the tree subtracting each node value from the target and check for zero at a leaf.
A Branch Whose Loads Add Up is our own problem on the same technique: a new statement, graded in your browser.
05Pattern
Traversal & Search
Query trees using traversal and BST order
When to reach for it
- You need to search for a specific value or perform other queries on a binary tree, such as finding the lowest common ancestor or the kth smallest/largest element.
Spot it in the prompt
Look for problems where you apply different traversal techniques or search algorithms specific to binary trees, such as inorder traversal or binary search.
Lowest Common Ancestor of a Binary Tree
How to solve it
Animation shows the Traversal & Search techniqueRecurse; if a node matches either target or its two subtrees both return non-null, that node is the LCA.
06Pattern
Validation & Properties
Verify structural properties of a tree
When to reach for it
- You need to validate properties of a binary tree, such as whether it's a binary search tree or a balanced tree.
Spot it in the prompt
Explore problems where you verify specific conditions or properties of binary trees, such as BST validation, balancedness checks, or identifying the diameter of the tree.
Diameter of Binary Tree
How to solve it
Animation shows the Validation & Properties techniqueCompute subtree heights via DFS while updating the max of left plus right height at each node.
Widest Gallery Walk is our own problem on the same technique: a new statement, graded in your browser.
After the pattern
Now solve one you haven't seen.
Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.
1,194 problems · 15,062 graded tests · free