Height and Depth of Binary Tree

The height of the tree is the height of the root node. The depth of the tree is the depth of the node that is farthest from the root in the downward direction.

The height of a particular node is the length of the longest downward path (also called edges) to a leaf from that node. The depth of a particular node is the length of the path (also called edges) from that node to the tree’s root (i.e., its root path).

It is easy to get confused between Depth and Height of Binary tree. It is because the depth of binary tree is always equal to the height of binary tree but they are not the same and using the terms interchangeably is not correct.

Lets understand the difference between the Height and Depth of a given node in the tree and height and depth of the Binary tree itself.

Dream as high as the sky and as deep as the ocean.

Height

  1. As the quote says, the sky is what we should see while calculating height (of a node or the root) from the sea-level. In our case, the sea-level is the leaf that is farthest from the particular node in the downward direction.
  2. The height of a particular node is the length of the longest downward path (also called edges) to a leaf from that node.
  3. The height of the root node of the tree is the height of the tree itself. The height of the root node is the highest because it is the farthest from the leaf node that is at the most bottom in the tree.
  4. The leaf nodes have height of 0 as there is no nodes below them.

Depth

  1. Think of ocean and the quote above while calculating depth.
  2. The depth of the ocean is calculated with respect to the sea level. In our case, the sea-level is the root of the tree.
  3. The depth of a particular node is the measure of how far that node is from the root of the tree.
  4. The depth of a particular node is the length of the path (also called edges) from that node to the tree’s root (i.e., its root path).
  5. The depth of binary tree is the depth of the deepest node (leaf node).
  6. The root node has a depth of 0 since it is at the sea-level.

How to calculate the depth of a binary tree?

At the tree level, the height and the depth are always going to have equal values (even thought they do not represent the same thing).

So, the same implementation for calculating “Height” is also applicable for calculating the “Depth” of a tree.

                    depth(of the root node)=0
                    height(of the tree)=3
                       /                \
                      /                  \
                     /                    \
                    /                      \
           depth(of the node)=1           depth(of the node)=1
           height(of the node)=1          height(of the node)=2
             /        \                        \
            /          \                        \
           /            \                        \
          /              \                        \
depth(of the node)=2    depth(of the node)=2       depth(of the node)=2
height(of the node)=0   height(of the node)=0      height(of the node)=1
                                                     /
                                                    /
                                                   /
                                                  /
                                          depth(of the tree)=3
                                          height(of the node)=0

Links to this note