Created
March 2, 2015 09:57
-
-
Save rohith2506/a75acd79fe5d3cf4130c to your computer and use it in GitHub Desktop.
diameter of a binary tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// diameter of binary tree | |
// consider a root --> diameter of a tree = max(if path goes through that root, if path does not go through that root) | |
// so diamter = max(lh+rh+1, max(ld,rd)); | |
int diameter(node *t){ | |
if(t != NULL){ | |
int lh = height(t -> left); | |
int rh = height(t -> right); | |
int ld = diameter(t -> left); | |
int rd = diameter(t -> right); | |
return max(lh+rh+1, max(ld, rd)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment