What is Height in a Binary Tree?
Let’s take a binary tree:
Think of height like climbing up. The higher we go, the greater the height.
- If we stand on the ground, our height is 0.
- In the same way, in a binary tree, the height of a leaf node is 0.
- We measure height from bottom to top by taking the longest path.
📌 Definition: The height of a binary tree is the number of edges on the longest path from the root to a leaf node.
👉 In the first image, the height of the tree is 3 because the longest path from root to a leaf has 3 edges.
What is Depth in a Binary Tree?
Depth tells us how deep a node is in the tree.
Let’s think of a pond:
- The more we dive under the water, the deeper we go.
- Similarly, in a tree, the depth of the root node is 0.
- As we move downward, the depth increases.
📌 Definition: The depth of a node is the number of edges from the root to that node.
👉 In the first image, the maximum depth is 3, which means the deepest node is 3 edges away from the root.
Another Example
In this tree:
- The height is 4 (longest path from root to leaf has 4 edges).
- The maximum depth is also 4 (deepest node is 4 edges away from the root).