-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiameter of binary tree.cpp
33 lines (27 loc) · 1.21 KB
/
diameter of binary tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int height(TreeNode* root,int& diameter) // passing value by refernce as we need to change it everytime
{
if(root==NULL) return 0;
int lh = height(root->left,diameter); // using recursion calling the height fuction to find height of left tree
int rh = height(root->right,diameter); // using recursion calling the height fuction to find height of left tree
diameter = max(diameter,rh+lh); // at every junction the diamter should be max of diamter and sum of lh and rh
return 1+max(lh,rh); // returning as 1 + lh,rh same as used in finding the height of binary tree
}
int diameterOfBinaryTree(TreeNode* root) {
int diameter = 0; // declaring diameter and making it to zero
height(root,diameter); // calling a function height
return diameter; // returning diamter
}
};