File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments