Diameter of a binary tree
The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree.
Write a program to find the diameter of a binary tree. |
Write a program to find the diameter of a binary tree. |
int diameter(Tree * t)
ReplyDelete{
if (t == 0) return 0;
int lheight = height(tree->left);
int rheight = height(tree->right);
int ldiameter = diameter(tree->left);
int rdiameter = diameter(tree->right);
return max(lheight + rheight + 1,
max(ldiameter,rdiameter));
}