We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bbc60df commit afc84eaCopy full SHA for afc84ea
17 - Binary Tree Data Structure Problems/06 - Diameter of a Binary Tree/main.cpp
@@ -0,0 +1,28 @@
1
+class Solution {
2
+ public:
3
+ pair<int, int> diameterFast(Node* root){
4
+ // base case
5
+ if(root == NULL){
6
+ pair<int, int> p = make_pair(0, 0);
7
+ return p;
8
+ };
9
+
10
+ pair<int, int> left = diameterFast(root -> left);
11
+ pair<int, int> right = diameterFast(root -> right);
12
13
+ int op1 = left.first;
14
+ int op2 = right.first;
15
+ int op3 = left.second + right.second + 1;
16
17
18
+ pair<int, int> ans;
19
+ ans.first = max(op1, max(op2, op3));
20
+ ans.second = max(left.second, right.second) + 1;
21
22
+ return ans;
23
+ }
24
25
+ int diameter(Node* root) {
26
+ return diameterFast(root).first;
27
28
+};
0 commit comments