Skip to content

Commit

Permalink
added problems for sept 11
Browse files Browse the repository at this point in the history
  • Loading branch information
whogurdevil committed Sep 11, 2023
1 parent 0bfd750 commit 00c865f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ July 16-17 | [celebrity.cpp](https://github.com/whogurdevil/Problems/blob/main/g
| July 22 | [dequeue.cpp](https://github.com/whogurdevil/Problems/blob/main/codingninja/dequeue.cpp) | [CodingNinjas](https://www.codingninjas.com/studio/problems/deque_1170059) | Queues |
| July 23 | [reverseQueue.cpp](https://github.com/whogurdevil/Problems/blob/main/geeksforgeeeks/reverseQueue.cpp) [kReverseQueue.cpp](https://github.com/whogurdevil/Problems/blob/main/geeksforgeeeks/kReverseQueue.cpp) | [GeeksForGeeks](https://practice.geeksforgeeks.org/problems/queue-reversal/1) [GeeksForGeeks](https://practice.geeksforgeeks.org/problems/reverse-first-k-elements-of-queue/1) | Queues |
| July 24-25 | [negativeInteger.cpp](https://github.com/whogurdevil/Problems/blob/main/geeksforgeeeks/negativeInteger.cpp) | [GeeksForGeeks](https://practice.geeksforgeeks.org/problems/first-negative-integer-in-every-window-of-size-k3345/1) | Queue |
| July 26 | [repeatingChar.cpp](https://github.com/whogurdevil/Problems/blob/main/geeksforgeeeks/repeatingChar.cpp) |[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-stream1216/1) | Queues |
| July 26 | [repeatingChar.cpp](https://github.com/whogurdevil/Problems/blob/main/geeksforgeeeks/repeatingChar.cpp) |[GeeksForGeeks](https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-stream1216/1) | Queues |
| Sept 11 | [heightOfTree.cpp]() | [GeeksForGeeks](https://practice.geeksforgeeks.org/problems/height-of-binary-tree/1) | Trees |
129 changes: 129 additions & 0 deletions geeksforgeeeks/heightOfTree.cpp
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

0 comments on commit 00c865f

Please sign in to comment.