TusharPatil
BAN USERPassed out Msc computer science from Pune University Computer Science Department.
It doesn't work if (min & max ) < root->data || > root->data
Here range must be min < root < max like this.
I think this is not a target solution, Can u improve it.
@adam2008
If u calculate height and diameter in one traverse it will improve the complexity, it behaves in O(n) currently it bahaves in O(n^2).
int height(mynode *ptr) //supporting height function {
int lheight=0,rheight=0;
if(ptr == NULL)
return 0;
lheight = height(ptr->left);
rheight = height(ptr->right);
if(lheight >= rheight)
return (lheight+1);
else
return (rheight+1);}
int max(int a, int b){ //supporting max function
if(a>b)
return a;
else
return b;}
int Diameter(mynode *root){
if(root == NULL)
return 0;
int lheight = height(root->left);
int rheight = height(root->right);
int ldiam = Diameter(root->left); //calculate left subtree diam
int rdiam = Diameter(root->right); //calculate right subtree diam
return max(lheight+rheight+1, max(ldiam,rdiam));
}
Nice solution dude, keep it up..
- TusharPatil February 19, 2013
RepSaanviJones, abc at 8x8
I am working as an office worker in a softage company. I am responsible for an office assistant position in ...
- TusharPatil March 06, 2013