CMD Guide
HomeDSATrees

Introduction to Tree

What a Tree Actually Is

A tree is a set of nodes connected by directed parent→child edges such that exactly one node (the root) has no parent, and every other node has exactly one parent reachable from the root by a unique path. That single constraint — one path from root to any node, no cycles, no node with two parents — is what makes a tree a restricted, acyclic connected graph rather than a general graph: it guarantees recursive self-similarity (every node's descendants form a smaller tree, a subtree), which is why almost every tree algorithm is written as "solve it at this node using the answers already solved for its children."

Recognize the pattern

Terminology (precise, not just vibes)

TermDefinition
Depth of a node# edges from root to that node
Height of a node# edges on the longest downward path from that node to a leaf
Height of treeheight of the root
Levelset of nodes at the same depth (root = level 0)
Degree# children of a node
Full binary treeevery node has 0 or 2 children, never 1
Complete binary treeevery level full except possibly the last, which fills left-to-right (heaps use this)

Brute force vs. the tree's optimal shape

If you stored the same hierarchical data in a flat array and needed "find all descendants of X" or "is A an ancestor of B", you would linearly scan and reconstruct relationships every time — O(n) per query with extra bookkeeping. A tree makes ancestry structural: descendants are just "everything reachable by following child pointers", so a single DFS from X answers it in O(size of X's subtree), and parent/child lookup is O(1) via pointers instead of O(n) search through a flat structure.

Complexity, derived

For a tree with n nodes and height h: a full traversal (DFS or BFS) visits each node once and follows each edge once → exactly n node-visits and n−1 edge-traversals, giving O(n) time. Space is O(h) for DFS (the recursion/call stack holds at most one frame per level on the current root-to-leaf path) and O(w) for BFS where w is the tree's maximum width (the queue holds one full level at a time).

The height-vs-n relationship depends on exactly what "balanced" means, and the geometric-sum argument below applies only to a complete (or full) binary tree, where level i holds up to 2i nodes: summing levels 0..h gives n ≤ 2h+1−1, so h = ⌊log₂ n⌋ exactly. Real-world "balanced" self-balancing BSTs only guarantee this order of growth, not the exact formula — an AVL tree's height is at most ≈1.44 log₂ n, and a red-black tree's is at most 2 log₂(n+1). The correct general statement is: a balanced tree has height h = Θ(log n); only a complete/full binary tree pins that down to the exact floor(log₂ n). For a degenerate tree (every node has one child, i.e. a linked list in disguise), h = n−1 — this is exactly why unbalanced BSTs degrade to O(n) search instead of O(log n).

Worked example: compute height by hand

Tree: A(root) → B, C  |  B → D, E  |  C → F.

NodeDepthHeightWhy
D, E, F20leaves — no children
B11max(height(D), height(E)) + 1 = 0+1
C11max(height(F), −1[no right child]) + 1 = 0+1
A02max(height(B)=1, height(C)=1) + 1

Tree height = 2. This is the recurrence height(node) = -1 if node is null else 1 + max(height(left), height(right)), evaluated bottom-up — the canonical example of solving a tree problem via post-order recursion.

Java: node definition and the height recurrence

class TreeNode {
    int val;
    TreeNode left, right;
    TreeNode(int val) { this.val = val; }
}

int height(TreeNode node) {
    if (node == null) return -1;          // convention: empty subtree has height -1
    return 1 + Math.max(height(node.left), height(node.right));
}

// Locates a specific node OBJECT, not a value. Uses reference equality
// (==) on purpose: root == target only matches if target is the exact
// node instance from this tree, not a different node that happens to
// hold an equal .val. If you need "find the node whose value equals X",
// compare root.val == target.val instead — that is a different,
// value-based search with different duplicate-handling semantics.
int depth(TreeNode root, TreeNode target, int d) {
    if (root == null) return -1;
    if (root == target) return d;
    int left = depth(root.left, target, d + 1);
    if (left != -1) return left;
    return depth(root.right, target, d + 1);
}

Pitfalls

When to use a tree / when not, and vs. alternatives

Takeaways

Recall: Why is DFS's space complexity O(height) rather than O(number of nodes), and what real-world input shape makes that bound approach O(n)?


Compiled from standard tree terminology (root, height, depth, full/complete binary trees) as used in CLRS-style treatments and common interview-prep references; height recurrence and complexity derivation are original to this page.

🤖 Don't fully get this? Learn it with Claude

Stuck on Introduction to Tree? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Introduction to Tree** (DSA) and want to truly understand it. Explain Introduction to Tree from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Introduction to Tree** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Introduction to Tree** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Introduction to Tree** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes