Skip to content

Commit 4328319

Browse files
authoredOct 14, 2022
Diameter of Binary Tree.cpp
1 parent 215e5b0 commit 4328319

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
 

‎Diameter of Binary Tree.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/* A binary tree node has data, pointer to left child
5+
and a pointer to right child */
6+
struct Node {
7+
int data;
8+
struct Node* left;
9+
struct Node* right;
10+
};
11+
Node* newNode(int val) {
12+
Node* temp = new Node;
13+
temp->data = val;
14+
temp->left = NULL;
15+
temp->right = NULL;
16+
return temp;
17+
}
18+
Node* buildTree(string str) {
19+
// Corner Case
20+
if (str.length() == 0 || str[0] == 'N') return NULL;
21+
22+
// Creating vector of strings from input
23+
// string after spliting by space
24+
vector<string> ip;
25+
26+
istringstream iss(str);
27+
for (string str; iss >> str;) ip.push_back(str);
28+
29+
// Create the root of the tree
30+
Node* root = newNode(stoi(ip[0]));
31+
32+
// Push the root to the queue
33+
queue<Node*> queue;
34+
queue.push(root);
35+
36+
// Starting from the second element
37+
int i = 1;
38+
while (!queue.empty() && i < ip.size()) {
39+
40+
// Get and remove the front of the queue
41+
Node* currNode = queue.front();
42+
queue.pop();
43+
44+
// Get the current node's value from the string
45+
string currVal = ip[i];
46+
47+
// If the left child is not null
48+
if (currVal != "N") {
49+
50+
// Create the left child for the current node
51+
currNode->left = newNode(stoi(currVal));
52+
53+
// Push it to the queue
54+
queue.push(currNode->left);
55+
}
56+
57+
// For the right child
58+
i++;
59+
if (i >= ip.size()) break;
60+
currVal = ip[i];
61+
62+
// If the right child is not null
63+
if (currVal != "N") {
64+
65+
// Create the right child for the current node
66+
currNode->right = newNode(stoi(currVal));
67+
68+
// Push it to the queue
69+
queue.push(currNode->right);
70+
}
71+
i++;
72+
}
73+
74+
return root;
75+
}
76+
/* Function to get diameter of a binary tree */
77+
int diameter(struct Node* tree);
78+
79+
/* Driver program to test size function*/
80+
int main() {
81+
int t;
82+
scanf("%d\n", &t);
83+
while (t--) {
84+
string s;
85+
getline(cin, s);
86+
Node* root = buildTree(s);
87+
cout << diameter(root) << endl;
88+
}
89+
return 0;
90+
}
91+
// } Driver Code Ends
92+
93+
94+
/* Tree node structure used in the program
95+
96+
struct Node
97+
{
98+
int data;
99+
struct Node* left;
100+
struct Node* right;
101+
102+
Node(int x){
103+
data = x;
104+
left = right = NULL;
105+
}
106+
}; */
107+
108+
/* Computes the diameter of binary tree with given root. */
109+
int height(Node* root)
110+
{
111+
if(root==NULL)
112+
return 0;
113+
114+
return 1+ max(height(root->left),height(root->right));
115+
}
116+
117+
int diameter(Node* root)
118+
{
119+
// Your code here
120+
if(root==NULL)
121+
return 0;
122+
int h1= height(root->left);
123+
int h2= height(root->right);
124+
125+
return max( max(h1+h2+1,diameter(root->left)) , diameter(root->right) );
126+
127+
}

0 commit comments

Comments
 (0)
Please sign in to comment.