From dc1de68b3cea7bde3c1ccdaf0280d44f9611e82a Mon Sep 17 00:00:00 2001 From: Abhishek Rai <39706860+ab-rai@users.noreply.github.com> Date: Sat, 2 May 2020 18:10:23 +0530 Subject: [PATCH 1/3] Create Day 11 Diameter of Binary Tree --- Day 11 Diameter of Binary Tree | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Day 11 Diameter of Binary Tree diff --git a/Day 11 Diameter of Binary Tree b/Day 11 Diameter of Binary Tree new file mode 100644 index 0000000..81efb58 --- /dev/null +++ b/Day 11 Diameter of Binary Tree @@ -0,0 +1,40 @@ +/* +#Difficulty Level=6/10 (requires good hands on experience of recursive functions call ) +Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. + +Example: +Given a binary tree + 1 + / \ + 2 3 + / \ + 4 5 +Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3]. +Note: The length of path between two nodes is represented by the number of edges between them. +*/ +class Solution { +public: + int findDiameter(TreeNode * root,int & mx) + { + if(root==NULL) return 0; + if(root->left==NULL && root->right==NULL) // it means this is a leaf node + { + mx=max(mx,0); // max path lenth for leaf node is 0 + return 1; + } + int leftMaxLen=findDiameter(root->left,mx); + int rightMaxLen=findDiameter(root->right,mx); + mx=max(mx,leftMaxLen+rightMaxLen); // adding the left and right lenth which is returned by the recursive call + return 1+(max(leftMaxLen,rightMaxLen));// returning maximum by adding 1 to the lenth of left and right since curr node is + } // also counted in the diameter + int diameterOfBinaryTree(TreeNode* root) { + int mx=0; + if(root==NULL) + return mx; + int len =findDiameter(root,mx); + return mx; + } +}; + +// Time Complexity:-O(n) each and every node is traversed once by the recursive call +// Time Complexity:-O(h) recursive stack of size h is used by recursive call,where h is the height of the binary tree From 46cb0aae267c9dc1a5001f10de8028153fb47ab1 Mon Sep 17 00:00:00 2001 From: Abhishek Rai <39706860+ab-rai@users.noreply.github.com> Date: Sat, 2 May 2020 18:19:09 +0530 Subject: [PATCH 2/3] Add files via upload --- abstr.cpp | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 abstr.cpp diff --git a/abstr.cpp b/abstr.cpp new file mode 100644 index 0000000..3a6af62 --- /dev/null +++ b/abstr.cpp @@ -0,0 +1,6 @@ +#include +using namespace std; +int main(){ + cout<<"Check Str"; + return 0; +} \ No newline at end of file From 7a98eaefa12c530d1fe5b0a46b477c7c3c41bfb7 Mon Sep 17 00:00:00 2001 From: Abhishek Rai <39706860+ab-rai@users.noreply.github.com> Date: Sat, 2 May 2020 18:21:24 +0530 Subject: [PATCH 3/3] Delete abstr.cpp --- abstr.cpp | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 abstr.cpp diff --git a/abstr.cpp b/abstr.cpp deleted file mode 100644 index 3a6af62..0000000 --- a/abstr.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -using namespace std; -int main(){ - cout<<"Check Str"; - return 0; -} \ No newline at end of file