top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

ALGO: What is the diameter of a tree and what is the way to find it ?

0 votes
386 views
ALGO: What is the diameter of a tree and what is the way to find it ?
posted Apr 8, 2016 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

**

Diam­e­ter of tree:

**
A longest path or route between any two nodes in a tree. The path may or may not for through the root.
And we find diameter as:
Maximum(Diameter of left sub­tree, Diam­e­ter of right sub­tree, Longest path between two nodes which passes through the root.)
Now the diam­e­ter of left and right subtree’s can be solved recur­sively. And Longest path between two nodes which passes through the root can be cal­cu­lated as 1 + height of left sub­tree + height of right subtree.

answer Apr 10, 2016 by Ajay Kumar
Similar Questions
+2 votes

1,1,2,2,2,6,6,6,7,7,7,7,7,7,7,8,8,9,9,9

Example:
Input = 1 Output=0 (First index of 1).
Input = 2 Output=2 (First index of 2).
Input = 6 Output= 5 (First index of 6).
Input = 7 Output= 8 (First index of 7).
Input = 8 Output=15 (First index of 8).
Input = 9 Output=17 (First index of 9).

+7 votes

A binary tree is given and two nodes of this tree is given, we have to find out the algorithm/code for lowest common ancestor of the two given nodes. Common ancestor are the nodes which can be reached from two nodes by following the parent links. Lowest common ancestor is the common ancestor which has the highest depth.

...