DEV Community

Mujahida Joynab
Mujahida Joynab

Posted on

Height and Depth of a binary Tree

What is Height in a Binary Tree?

Let’s take a binary tree:

Image description

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

Image description

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).

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay