Browse curriculum

Binary Tree

A binary tree is a hierarchical structure in which each element has at most two children, referred to as the left child and the right child.

ANY

Any binary tree: every node has at most two children, and nothing else is promised.

Shape

Intuition

A hierarchical structure where each element has at most two children, referred to as the left child and the right child.

Concept

The foundation of many advanced data structures like BST, Heaps, and AVL Trees. There are several variations: Full (0 or 2 children), Perfect (all levels filled), Complete (filled level by level), Balanced (height diff ≤ 1), and Skewed (degenerate tree).

How it Works

  • Root: Topmost node.
  • Child: Node connected downwards.
  • Leaf: Node with no children.
  • Height: Longest path from node to leaf.
  • Depth: Path length from root to node.

Step-by-Step Breakdown

Common Operations:
  1. Traversal: Visiting nodes (Pre-order, In-order, Post-order, Level-order).
  2. Insertion: Adding a node (logic varies by tree type).
  3. Deletion: Removing a node (often replacing with leaf or child).
  4. Height/Depth: Calculating tree metrics.

When to Use

  • Representing hierarchical data (file systems, HTML DOM).
  • Decision trees.
  • Expression parsers.
  • Binary Heaps (Complete Trees).

When NOT to Use

  • When data is linear (use Array/Linked List).
  • When relationships are complex/cyclic (use Graph).

How to Identify

Any tree structure where max degree of any node is 2.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

What is a Binary Tree?

A Binary Tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child.


Root vs Leaf nodes?

The Root is the top-most node of the tree. A Leaf is a node that has no children (the "ends" of the branches).


What is the "Height" of a tree?

Tree Height is the number of edges on the longest path from the root to a leaf node.