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 a04b7ed commit e4c8ee5Copy full SHA for e4c8ee5
17 - Binary Tree Data Structure Problems/06 - Diameter of a Binary Tree/Worst Approach.cpp
@@ -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
16
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