Trees and Binary Search Trees are hierarchical data structures fundamental to computer science, storing data in parent-child relationships rather than linear sequences. While a binary tree simply restricts each node to at most two children, a Binary Search Tree (BST) imposes an ordering constraint — left children hold smaller values, right children hold larger values — enabling logarithmic-time operations when balanced. Understanding traversal patterns, balancing mechanisms, and structural properties is essential for efficient searching, sorting, and hierarchical data manipulation across countless algorithms and applications.
What This Cheat Sheet Covers
This topic spans 15 focused tables and 105 indexed concepts, 93 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Core Tree Terminology
Before reasoning about any tree algorithm you need a shared vocabulary, and these terms are it — root, leaf, height, depth, and the rest. Get the difference between height and depth straight here (one counts down toward leaves, the other counts up from the root) and most later confusion disappears.
| Term | Example | Description | |
|---|---|---|---|
root = TreeNode(10) | • The topmost node with no parent • serves as the single entry point for all tree operations. | ||
node.left == None and node.right == None | • A node with no children • appears at the tree's periphery and marks endpoints in traversals. | ||
Any node with at least one child | • A node with one or two children • forms the structural backbone connecting root to leaves. | ||
height = max(left_height, right_height) + 1 | • Number of edges on the longest path from a node to any descendant leaf • the root's height equals the tree's height. | ||
Depth of root is 0 | • Number of edges from the root to a specific node • increases by 1 at each level downward. | ||
Root is at level 0 | • The set of all nodes at the same depth • level-order traversals process one level at a time. | ||
Connection between parent and child | • The link between two nodes representing a parent-child relationship • a tree with $n$ nodes has exactly $n-1$ edges. | ||
subtree = node.left | • A node and all its descendants • every non-leaf node is the root of a left and right subtree. | ||
Root → child → grandchild | • A sequence of nodes connected by edges between two nodes • path length = number of edges traversed. | ||
Binary tree nodes have degree ≤ 2 | • The number of children a node has • binary trees restrict degree to a maximum of 2. | ||
All nodes on path from root to node | • Any node on the path from root to a given node • used in lowest common ancestor (LCA) queries. | ||
All nodes in node's subtree | • Any node reachable by moving downward through children • the opposite relationship of ancestor. | ||
Nodes sharing the same parent | • Two nodes with the same parent node • left and right children are always siblings of each other. |