Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DSA
Data Structures & Algorithms
0/15 completed
Beginner
Big-O and complexityArrays and stringsHash maps and setsTwo pointers and sliding windows
Intermediate
Linked listsStacks and queuesRecursion and backtrackingSorting algorithmsBinary search
Advanced
Trees and binary search treesHeaps and priority queuesGraphs: BFS and DFSDynamic programming
Pro
Shortest paths, topological sort and union-findSolving problems in coding interviews
Tracks / Data Structures & Algorithms / Advanced

Trees and binary search trees

A tree is a hierarchy: a root node with children, which have children of their own. File systems, HTML pages (the DOM), company org charts, JSON documents and database indexes are all trees. A binary tree gives each node at most two children, and a binary search tree (BST) keeps them ordered so searching is fast.

          8            ← root
        /   \
       3     10        ← children of 8
      / \      \
     1   6      14     ← leaves: 1, 4, 7, 13
        / \    /
       4   7  13
Term Meaning
Root / leaf The top node / a node with no children
Depth of a node Edges from the root to it
Height of a tree The depth of its deepest leaf
Subtree A node and all its descendants
Balanced Left and right heights differ little everywhere, so height ≈ log n

Building a tree and traversing it

Python · editable

These three are depth-first traversals. Breadth-first (level-order) traversal visits the tree level by level, using a queue:

Python · editable

Thinking recursively about trees

Most tree problems follow one shape: solve it for the left subtree, solve it for the right subtree, combine:

Python · editable

Binary search trees

In a BST, for every node: everything in the left subtree is smaller, everything in the right subtree is larger. Searching is like binary search: each comparison discards a whole subtree.

Python · editable

A BST's operations cost O(height). Balanced, that's O(log n); but inserting sorted data builds a "tree" that's really a linked list, height n, and everything becomes O(n). Self-balancing trees fix this by rotating nodes after inserts: AVL trees, red-black trees (Java's TreeMap, C++'s std::map) and B-trees (database indexes, which keep hundreds of keys per node so the tree stays only 3–4 levels deep).

Validating a BST

A favourite interview trap: checking only that each node is bigger than its left child and smaller than its right child is not enough. Every node must fit the range allowed by all its ancestors:

Python · editable

Lowest common ancestor

Python · editable

Tries: trees for strings

A trie (prefix tree) stores strings character by character, so words sharing a prefix share a path. It's what powers autocomplete:

Python · editable

Lookups cost O(length of the word), regardless of how many words are stored.

Practice

  1. Write is_balanced(root): every node's subtrees differ in height by at most 1 (do it in one pass).
  2. Find the k-th smallest key in a BST using an in-order traversal.
  3. Serialise a binary tree to a string and rebuild it.
✎ Check your understanding

1.Which traversal visits a BST's keys in sorted order?

2.Why can a BST degrade to O(n) operations?

3.What makes tries good for autocomplete?

← Binary search