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

This patternO(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.

Easy

Binary Tree Inorder Traversal

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Traversal technique

Recurse 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Medium

Construct Binary Tree from Preorder and Inorder Traversal

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Construction technique

Take 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.

Practice · 3 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Easy

Invert Binary Tree

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Mirror & Symmetry technique

Recursively 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Loading animation…

How to solve it

Animation shows the Path Sum & Root to Leaf technique

DFS 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Easy

Diameter of Binary Tree

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Validation & Properties technique

Compute 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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