Skip to content

Commit

Permalink
Adding codes for Lecture 64
Browse files Browse the repository at this point in the history
  • Loading branch information
Love Babbar committed Feb 18, 2022
1 parent 7e3a9ff commit b6b8347
Show file tree
Hide file tree
Showing 7 changed files with 1,161 additions and 0 deletions.
147 changes: 147 additions & 0 deletions Lecture064 Tree Interview Questions Day2/LeftView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#include <bits/stdc++.h>
using namespace std;

// Tree Node
struct Node
{
int data;
Node* left;
Node* right;
};

vector<int> leftView(struct Node *root);

// Utility function to create a new Tree Node
Node* newNode(int val)
{
Node* temp = new Node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;

return temp;
}


// 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);

// for(string i:ip)
// cout<<i<<" ";
// cout<<endl;
// Create the root of the tree
Node* root = newNode(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 = newNode(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 = newNode(stoi(currVal));

// Push it to the queue
queue.push(currNode->right);
}
i++;
}

return root;
}


int main() {
int t;
scanf("%d ",&t);
while(t--)
{
string s;
getline(cin,s);
Node* root = buildTree(s);
vector<int> vec = leftView(root);
for(int x : vec)
cout<<x<<" ";
cout << endl;
}
return 0;
}

// } Driver Code Ends


/* A binary tree node
struct Node
{
int data;
struct Node* left;
struct Node* right;
Node(int x){
data = x;
left = right = NULL;
}
};
*/

//Function to return a list containing elements of left view of the binary tree.
void solve(Node* root, vector<int> &ans, int level) {
//base case
if(root == NULL)
return ;

//we entered into a new level
if(level == ans.size())
ans.push_back(root->data);

solve(root->left, ans, level+1);
solve(root->right, ans, level+1);
}

vector<int> leftView(Node *root)
{
vector<int> ans;
solve(root, ans, 0);
return ans;
}
152 changes: 152 additions & 0 deletions Lecture064 Tree Interview Questions Day2/bottomView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include <bits/stdc++.h>
using namespace std;
#define MAX_HEIGHT 100000

// Tree Node
struct Node
{
int data;
Node* left;
Node* right;
};

// Utility function to create a new Tree Node
Node* newNode(int val)
{
Node* temp = new Node;
temp->data = val;
temp->left = NULL;
temp->right = NULL;

return temp;
}


vector <int> bottomView(Node *root);

// 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 = newNode(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 = newNode(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 = newNode(stoi(currVal));

// Push it to the queue
queue.push(currNode->right);
}
i++;
}

return root;
}


// } Driver Code Ends
//Function to return a list containing the bottom view of the given tree.

class Solution {
public:
vector <int> bottomView(Node *root) {
vector<int> ans;
if(root == NULL)
{
return ans;
}

map<int,int> topNode;
queue<pair<Node*, int> > q;

q.push(make_pair(root, 0));

while(!q.empty()) {
pair<Node*, int> temp = q.front();
q.pop();
Node* frontNode = temp.first;
int hd = temp.second;

topNode[hd] = frontNode -> data;

if(frontNode->left)
q.push(make_pair(frontNode->left, hd-1));
if(frontNode->right)
q.push(make_pair(frontNode->right, hd+1));
}

for(auto i:topNode)
{
ans.push_back(i.second);
}
return ans;
}
};

// { Driver Code Starts.

int main() {
int t;
string tc;
getline(cin, tc);
t=stoi(tc);
while(t--)
{
string s ,ch;
getline(cin, s);
Node* root = buildTree(s);
Solution ob;
vector <int> res = ob.bottomView(root);
for (int i : res) cout << i << " ";
cout << endl;
}
return 0;
}


// } Driver Code Ends
Loading

0 comments on commit b6b8347

Please sign in to comment.