Maximum height (depth) of a binary tree

Given a binary tree, find its maximum height.

The maximum height of a binary tree is defined as the number of nodes along the path from the root node to the deepest leaf node. Note that the maximum height of an empty tree is 0.

C++:
01 int maxHight(Node* head)
02 {
03     if(head==NULL)
04         return 0;
05     int left=maxHight(head->left);
06     int right=maxHight(head->right);
07     if(left>=right)
08         return left+1;
09     else
10         return right+1;
11 }
Advertisements
This entry was posted in Recursive, Tree. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s