Skip to content

Commit e1e7155

Browse files
authored
Merge pull request #549 from NehaKharu/main
Diameter of Binary Tree.cpp
2 parents 0782b66 + 4ef6209 commit e1e7155

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-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+
}

Height of Binary tree.cpp

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

0 commit comments

Comments
 (0)