Given a binary tree how to find the height of the tree.

It is defined as the number of nodes in the longest path from root to any leaf.

We can assume that the height of empty tree is 0 and the height of a tree with just root node as 1. For example the below tree has a height of 4.

We can calculate the height of the tree using recursive method very easily. It is based on the observation that height of a binary tree is

1 + Max( height(left-sub-tree), height(right-sub-tree) )

Here is the C++ code which implements this simple algorithm.