ReviseAlgo Logo

Trees

Binary Tree Fundamentals

Master Binary Tree foundations: nodes, parent-child structures, height vs depth, and pre/in/post-order traversal invariants.

Last Updated: August 2, 2026 15 min read

1. Introduction

What is a Binary Tree?

A Binary Tree is a hierarchical data structure composed of nodes, where each node has at most two children, referred to as the left child and the right child.

Why is it Important?

Linear data structures like arrays and linked lists store data sequentially. Trees store data hierarchically, which mirrors real-world relationships. Furthermore, structured trees (like Binary Search Trees) enable operations like search, insertion, and deletion to run in O(log N) logarithmic time.

Where is it Used?

  • Document Object Model (DOM): Web browsers parse HTML code into a hierarchical tree of elements.
  • Decision Trees: Machine Learning algorithms branch decisions based on feature checks.

  • 2. Mental Model: The Corporate Org Chart

    Think of a Binary Tree as a corporate org chart:

  • The CEO sits at the very top (the Root).
  • The CEO has at most two direct reports (the left and right Children).
  • Every manager is themselves the "head" of their own department (a Subtree).
  • Individual contributors at the bottom who have no reports are called Leaf nodes.

  • 3. Core Terminology & Node Implementations

    Structural Definitions

  • Root: The topmost node in the tree (has no parent).
  • Leaf: A node that has no children (both left and right pointers are null).
  • Height: The number of edges on the longest path from a node to a leaf. The height of a single-node tree is 0, and a null tree has height -1 (or 0 depending on convention).
  • Depth: The number of edges from the root to the node.
  • Balanced: A tree where the heights of the left and right subtrees of every node differ by at most 1.
  • Complete: All levels are completely filled except possibly the last level, which is filled from left to right.
  • Node Definitions & Height Code


    4. Visualizing Height vs Depth

    Consider the tree below showing height (measured upwards from leaves) and depth (measured downwards from root):


    5. Real-World Examples

  • Folder Directories: Operating system file managers store folders and files inside nested hierarchical directories.
  • Routing Tables: Networking switches store IP prefixes using Trie trees to forward packets.

  • 6. Interview Perspective

    How Interviewers Ask This Topic

    Interviewers test your recursion boundaries:
  • "Given a binary tree, check if it is balanced." -> Write a helper that returns height. If left and right height differ by > 1, mark unbalanced.
  • "Find the maximum depth of a binary tree." -> Identical to calculating height. Return 1 + max(depth(left), depth(right)).
  • Common Mistakes

    Warning: 1. Missing Null Node Base Cases: Recursive calls must terminate. If you do not check if (root == null) at the start of your functions, your code will crash with a NullPointerException.
    > 2. Confusing Height and Depth: Remember that height is measured bottom-up (from leaf to node), while depth is measured top-down (from root to node).

    7. Summary

  • Binary Tree: Tree where nodes have ≤ 2 children.
  • Recursion: Trees are naturally recursive (every subtree is a tree).
  • Height: Longest path down to a leaf; depth is distance from root.
  • Complexity: Balanced trees have height O(log N); skewed trees have height O(N).

  • 8. Quiz

    Question 1: What is the maximum number of nodes in a binary tree of height H (where height of root is 0)? Answer: 2^H+1 - 1 nodes. For example, a tree of height 2 can hold at most 2^2+1 - 1 = 7 nodes.
    Question 2: What is the height of a single root node with no children? Answer: 0 (or 1 if counting nodes instead of edges).
    Question 3: Why does a skewed binary tree resemble a Singly Linked List? Answer: If every node in a binary tree only has a right child (or only a left child), the nodes form a straight linear sequence, degrading all search operations to O(N) linear scans.
    Question 4: True or False: Every binary tree must contain at least one leaf node. Answer: False. An empty tree (where root == null) has 0 nodes and therefore contains no leaf nodes.
    Question 5: What is the relationship between the number of leaves L and nodes with 2 children N2 in any binary tree? Answer: L = N_2 + 1. A tree always has exactly one more leaf node than it has nodes with two children.