← All Posts
DSA Series · Trees · Part 1

Introduction to Trees

What is a Tree?

A tree is a connected, acyclic graph where one distinguished node is called the root. Every other node has exactly one parent, and zero or more children. Trees appear everywhere: file systems, the DOM, compilers (ASTs), databases (B-trees), and network routing.

10 5 15 3 7 12 20
Figure 1: A binary search tree with 7 nodes. Root = 10.

▶ Build This Tree Step-by-Step

Watch how a binary search tree is constructed by inserting nodes one at a time.

Key Terminology

depth 0 depth 1 depth 2 depth 3 A B C D E F G Root Leaf
Figure 2: Tree anatomy, depth levels, root (A), internal nodes (B, C, D), and leaves (E, F, G). Height of tree = 3.

Binary Trees

A binary tree is a tree where each node has at most two children, called left and right.

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
};

Types of Binary Trees

Full Binary Tree 1 2 3 4 5 6 7 Every node: 0 or 2 children Complete Binary Tree 1 2 3 4 5 6 Filled left-to-right, last level may be partial Degenerate (Skewed) 1 2 3 4 Every node has 1 child, O(n) height
Figure 3: Three categories of binary trees side by side.

▶ Interactive Depth & Height Explorer

Click any node to see its depth (from root) and height (to deepest leaf). The path lights up.

A B C D E F G
Click a node above ↑

Summary