Skip to content

Commit e4c8ee5

Browse files
authored
Create Worst Approach.cpp
1 parent a04b7ed commit e4c8ee5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
private:
3+
int height(Node* root) {
4+
// base case
5+
if(root == NULL) return 0;
6+
7+
int left = height(root -> left);
8+
int right = height(root -> right);
9+
10+
return max(left, right) + 1;
11+
}
12+
public:
13+
// Function to return the diameter of a Binary Tree.
14+
int diameter(Node* root) {
15+
// base case
16+
if(root == NULL) return 0;
17+
18+
int opt1 = diameter(root -> left);
19+
int opt2 = diameter(root -> right);
20+
int opt3 = height(root->left) + height(root -> right) + 1;
21+
22+
23+
return max(opt1, max(opt2, opt3));
24+
}
25+
};

0 commit comments

Comments
 (0)