Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 2, 2015 09:57
Show Gist options
  • Save rohith2506/a75acd79fe5d3cf4130c to your computer and use it in GitHub Desktop.
Save rohith2506/a75acd79fe5d3cf4130c to your computer and use it in GitHub Desktop.
diameter of a binary tree
// 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