From 09bcb9a4f514ec6fbd04d3930cff06ff7e5abf22 Mon Sep 17 00:00:00 2001 From: divy-ansh <72247466+divy-ansh@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:05:32 +0530 Subject: [PATCH] Add files via upload --- Trees and Graphs/Diameter of Binary Tree.txt | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Trees and Graphs/Diameter of Binary Tree.txt 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