Popular Posts

Friday, February 4, 2011

Diameter of a Binary Tree

Tree Diameter :The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree.The diameter of G is defined as being the maximal distance in the graph.
The diameter of a tree T is the largest of the following quantities :
* the diameter of T’s left subtree
* the diameter of T’s right subtree
* the longest path between leaves that goes through the root of T (this can be computed

/* Function to get diameter of a binary tree */
int diameter(struct node * tree)
{
   /* base case where tree is empty */
   if (tree == 0)
     return 0;
  /* get the height of left and right sub-trees */
  int lheight = height(tree->left);
  int rheight = height(tree->right);
  /* get the diameter of left and right sub-trees */
  int ldiameter = diameter(tree->left);
  int rdiameter = diameter(tree->right);
  /* Return max of following three
   1) Diameter of left subtree
   2) Diameter of right subtree
   3) Height of left subtree + height of right subtree + 1 */
  return max(lheight + rheight + 1, max(ldiameter, rdiameter));
}
Refer :  http://geeksforgeeks.org/?p=5687


No comments:

Post a Comment