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