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 inO(log N) logarithmic time.
Where is it Used?
2. Mental Model: The Corporate Org Chart
Think of a Binary Tree as a corporate org chart:
3. Core Terminology & Node Implementations
Structural Definitions
null).0, and a null tree has height -1 (or 0 depending on convention).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
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test your recursion boundaries:> 1, mark unbalanced.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
≤ 2 children.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 toO(N) linear scans.
Question 4: True or False: Every binary tree must contain at least one leaf node.
Answer: False. An empty tree (whereroot == 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.