diff --git a/Trees and Graphs/Diameter of Binary Tree.txt b/Trees and Graphs/Diameter of Binary Tree.txt new file mode 100644 index 0000000..8d72ca1 --- /dev/null +++ b/Trees and Graphs/Diameter of Binary Tree.txt @@ -0,0 +1,21 @@ +#include +int height(Node *n) +{ + if(n==NULL) + return 0; + + return 1+max(height(n->left),height(n->right)); +} +int diameter(Node* node) { + // Your code here + if(node==NULL) + return 0; + + int lh=height(node->left); + int rh=height(node->right); + + int ld=diameter(node->left); + int rd=diameter(node->right); + + return max(1+lh+rh,max(ld,rd)); +} \ No newline at end of file