Microsoft Interview Question
1,272 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Developer Engineer In Test at Microsoft:
Find diameter of binary tree.
See more for this Microsoft Software Developer Engineer In Test Interview
Helpful Question?
Yes |
No
Inappropriate?



0 of 0 people found this helpful
by Interview Candidate:
{
return (a >= b)? a: b;
}
int maxDepth(struct bTreeNode *root)
{
if(root == NULL)
{
return 0;
}
else
{
int ld = maxDepth(root->lNode);
int rd = maxDepth(root->rNode);
if( ld > rd)
return ld+1;
else
return rd+1;
}
}
int diameter(struct bTreeNode *root)
{
if(root == NULL)
{
return 0;
}
else
{
int ld = diameter(root->lNode);
int rd = diameter(root->rNode);
int lh = maxDepth(root->lNode);
int rh = maxDepth(root->rNode);
return max(lh + rh + 1, max(ld, rd));
}
}