Skip to content

Commit 795e916

Browse files
committed
Diameter of Binary Trees
1 parent 7811510 commit 795e916

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Trees/diameter_of_Binary_trees.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Node{
4+
int data;
5+
Node* left;
6+
Node* right;
7+
Node(int val) : data(val), left(nullptr), right(nullptr) {}
8+
};
9+
int ans;
10+
int depth(Node * root){
11+
if(root == NULL){
12+
return 0;
13+
}
14+
int left = depth(root->left);
15+
int right = depth(root->right);
16+
ans = max(ans , left + right);
17+
return max(left, right) + 1;
18+
}
19+
int diamterofBinaryTree(Node * root){
20+
ans = 0;
21+
depth(root);
22+
return ans;
23+
}
24+
int main(){
25+
Node * root = new Node(1);
26+
root->left = new Node(2);
27+
root->right = new Node(3);
28+
root->left->left = new Node(4);
29+
root->left->right = new Node(5);
30+
root->right->left = new Node(6);
31+
root->right->right = new Node(7);
32+
cout << "Diamter of the binary trees is : " << diamterofBinaryTree(root) << endl;
33+
return 0;
34+
}

0 commit comments

Comments
 (0)