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.

Comments

  1. int diameter(Tree * t)
    {
    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));
    }

    ReplyDelete

Post a Comment

Popular posts from this blog