-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bfd750
commit 00c865f
Showing
2 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
//{ Driver Code Starts | ||
//Initial template for C++ | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
int data; | ||
struct Node *left; | ||
struct Node *right; | ||
|
||
Node(int val) { | ||
data = val; | ||
left = right = NULL; | ||
} | ||
}; | ||
|
||
// Function to Build Tree | ||
Node* buildTree(string str) | ||
{ | ||
// Corner Case | ||
if(str.length() == 0 || str[0] == 'N') | ||
return NULL; | ||
|
||
// Creating vector of strings from input | ||
// string after spliting by space | ||
vector<string> ip; | ||
|
||
istringstream iss(str); | ||
for(string str; iss >> str; ) | ||
ip.push_back(str); | ||
|
||
// Create the root of the tree | ||
Node *root = new Node(stoi(ip[0])); | ||
|
||
// Push the root to the queue | ||
queue<Node*> queue; | ||
queue.push(root); | ||
|
||
// Starting from the second element | ||
int i = 1; | ||
while(!queue.empty() && i < ip.size()) { | ||
|
||
// Get and remove the front of the queue | ||
Node* currNode = queue.front(); | ||
queue.pop(); | ||
|
||
// Get the current node's value from the string | ||
string currVal = ip[i]; | ||
|
||
// If the left child is not null | ||
if(currVal != "N") { | ||
|
||
// Create the left child for the current Node | ||
currNode->left = new Node(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->left); | ||
} | ||
|
||
// For the right child | ||
i++; | ||
if(i >= ip.size()) | ||
break; | ||
currVal = ip[i]; | ||
|
||
// If the right child is not null | ||
if(currVal != "N") { | ||
|
||
// Create the right child for the current node | ||
currNode->right = new Node(stoi(currVal)); | ||
|
||
// Push it to the queue | ||
queue.push(currNode->right); | ||
} | ||
i++; | ||
} | ||
|
||
return root; | ||
} | ||
|
||
// } Driver Code Ends | ||
//User function template for C++ | ||
|
||
/* | ||
struct Node | ||
{ | ||
int data; | ||
struct Node* left; | ||
struct Node* right; | ||
Node(int x){ | ||
data = x; | ||
left = right = NULL; | ||
} | ||
}; | ||
*/ | ||
class Solution{ | ||
public: | ||
//Function to find the height of a binary tree. | ||
int height(struct Node* node){ | ||
if(node == NULL) { | ||
return 0; | ||
} | ||
|
||
int leftHeight = height(node->left); | ||
int rightHeight = height(node->right); | ||
|
||
return (max(leftHeight, rightHeight) + 1); | ||
} | ||
}; | ||
|
||
//{ Driver Code Starts. | ||
int main() | ||
{ | ||
int t; | ||
scanf("%d ",&t); | ||
while(t--) | ||
{ | ||
string treeString; | ||
getline(cin,treeString); | ||
Node* root = buildTree(treeString); | ||
Solution ob; | ||
cout<<ob.height(root)<<endl; | ||
} | ||
return 0; | ||
} | ||
// } Driver Code Ends |