From b6b8347d191491c1a3a7d9c4b2c612f9987b91e3 Mon Sep 17 00:00:00 2001 From: Love Babbar Date: Fri, 18 Feb 2022 16:03:54 +0530 Subject: [PATCH] Adding codes for Lecture 64 --- .../LeftView.cpp | 147 +++++++++++++ .../bottomView.cpp | 152 ++++++++++++++ .../boundaryTraversal.cpp | 197 ++++++++++++++++++ .../rightView.cpp | 154 ++++++++++++++ .../topView.cpp | 163 +++++++++++++++ .../verticalTraversal.cpp | 169 +++++++++++++++ .../zigzag.cpp | 179 ++++++++++++++++ 7 files changed, 1161 insertions(+) create mode 100644 Lecture064 Tree Interview Questions Day2/LeftView.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/bottomView.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/boundaryTraversal.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/rightView.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/topView.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/verticalTraversal.cpp create mode 100644 Lecture064 Tree Interview Questions Day2/zigzag.cpp diff --git a/Lecture064 Tree Interview Questions Day2/LeftView.cpp b/Lecture064 Tree Interview Questions Day2/LeftView.cpp new file mode 100644 index 00000000..3c35bd8c --- /dev/null +++ b/Lecture064 Tree Interview Questions Day2/LeftView.cpp @@ -0,0 +1,147 @@ +#include +using namespace std; + +// Tree Node +struct Node +{ + int data; + Node* left; + Node* right; +}; + +vector 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 ip; + + istringstream iss(str); + for(string str; iss >> str; ) + ip.push_back(str); + + // for(string i:ip) + // cout< 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 vec = leftView(root); + for(int x : vec) + cout< &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 leftView(Node *root) +{ + vector ans; + solve(root, ans, 0); + return ans; +} diff --git a/Lecture064 Tree Interview Questions Day2/bottomView.cpp b/Lecture064 Tree Interview Questions Day2/bottomView.cpp new file mode 100644 index 00000000..05ef8ed4 --- /dev/null +++ b/Lecture064 Tree Interview Questions Day2/bottomView.cpp @@ -0,0 +1,152 @@ +#include +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 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 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 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 bottomView(Node *root) { + vector ans; + if(root == NULL) + { + return ans; + } + + map topNode; + queue > q; + + q.push(make_pair(root, 0)); + + while(!q.empty()) { + pair 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 res = ob.bottomView(root); + for (int i : res) cout << i << " "; + cout << endl; + } + return 0; +} + + + // } Driver Code Ends \ No newline at end of file diff --git a/Lecture064 Tree Interview Questions Day2/boundaryTraversal.cpp b/Lecture064 Tree Interview Questions Day2/boundaryTraversal.cpp new file mode 100644 index 00000000..c42be081 --- /dev/null +++ b/Lecture064 Tree Interview Questions Day2/boundaryTraversal.cpp @@ -0,0 +1,197 @@ +#include +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; +} + + +// 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 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 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 +/* A binary tree Node +struct Node +{ + int data; + Node* left, * right; +}; */ + +class Solution { +public: + void traverseLeft(Node* root, vector &ans) { + //base case + if( (root == NULL) || (root->left == NULL && root->right == NULL) ) + return ; + + ans.push_back(root->data); + if(root->left) + traverseLeft(root->left, ans); + else + traverseLeft(root->right, ans); + + } + + void traverseLeaf(Node* root, vector &ans) { + //base case + if(root == NULL) + return ; + + if(root->left == NULL && root->right == NULL) { + ans.push_back(root->data); + return; + } + + traverseLeaf(root->left, ans); + traverseLeaf(root->right, ans); + + } + + void traverseRight(Node* root, vector &ans) { + //base case + if( (root == NULL) || (root->left == NULL && root->right == NULL) ) + return ; + + if(root->right) + traverseRight(root->right, ans); + else + traverseRight(root->left, ans); + + //wapas aagye + ans.push_back(root->data); + + } + + vector boundary(Node *root) + { + vector ans; + if(root == NULL) + return ans; + + ans.push_back(root->data); + + //left part print/store + traverseLeft(root->left, ans); + + //traverse Leaf Nodes + + //left subtree + traverseLeaf(root->left, ans); + //right subtree + traverseLeaf(root->right, ans); + + //traverse right part + traverseRight(root->right, ans); + + return ans; + + + } +}; + +// { Driver Code Starts. + +/* Driver program to test size function*/ + +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 res = ob.boundary(root); + for (int i : res) cout << i << " "; + cout << endl; + } + return 0; +} // } Driver Code Ends \ No newline at end of file diff --git a/Lecture064 Tree Interview Questions Day2/rightView.cpp b/Lecture064 Tree Interview Questions Day2/rightView.cpp new file mode 100644 index 00000000..2301ad74 --- /dev/null +++ b/Lecture064 Tree Interview Questions Day2/rightView.cpp @@ -0,0 +1,154 @@ +#include +using namespace std; + +// 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; +} + + + // } Driver Code Ends +/* A binary tree node has data, pointer to left child + and a pointer to right child +struct Node +{ + int data; + struct Node* left; + struct Node* right; + + Node(int x){ + data = x; + left = right = NULL; + } +}; */ + +// Should return right view of tree +class Solution +{ + public: + //Function to return list containing elements of right view of binary tree. + void solve(Node* root, vector &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->right, ans, level+1); + solve(root->left, ans, level+1); + +} + vector rightView(Node *root) + { + vector ans; + solve(root, ans, 0); + return ans; + } +}; + + + +// { Driver Code Starts. + +// 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 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 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; + string tc; + getline(cin,tc); + t=stoi(tc); + while(t--) + { + string s; + getline(cin,s); + Node* root = buildTree(s); + + Solution ob; + vector vec = ob.rightView(root); + for(int x : vec){ + cout< +using namespace std; + +// 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; +} + +// 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 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 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 + + +/* +struct Node +{ + int data; + Node* left; + Node* right; +}; +*/ +class Solution +{ + public: + //Function to return a list of nodes visible from the top view + //from left to right in Binary Tree. + vector topView(Node *root) + { + vector ans; + if(root == NULL) + { + return ans; + } + + map topNode; + queue > q; + + q.push(make_pair(root, 0)); + + while(!q.empty()) { + pair temp = q.front(); + q.pop(); + Node* frontNode = temp.first; + int hd = temp.second; + + //if one value is present for a HD, then do nothing + if(topNode.find(hd) == topNode.end()) + 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 tc; + cin>>tc; + cin.ignore(256, '\n'); + while (tc--) { + string treeString; + getline(cin, treeString); + Solution ob; + Node *root = buildTree(treeString); + vector vec = ob.topView(root); + for(int x : vec) + cout< +using namespace std; + +// 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; +} + +// 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 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 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; +} + +// Function for Inorder Traversal +void printInorder(Node* root) +{ + if(!root) + return; + + printInorder(root->left); + cout<data<<" "; + printInorder(root->right); +} + + + // } Driver Code Ends +class Solution +{ + public: + //Function to find the vertical order traversal of Binary Tree. + vector verticalOrder(Node *root) + { + map > > nodes; + queue< pair > > q; + vector ans; + + if(root == NULL) + return ans; + + q.push(make_pair(root, make_pair(0,0))); + + while(!q.empty()) { + pair > temp = q.front(); + q.pop(); + Node* frontNode = temp.first; + int hd = temp.second.first; + int lvl = temp.second.second; + + nodes[hd][lvl].push_back(frontNode->data); + + if(frontNode->left) + q.push(make_pair(frontNode->left, make_pair(hd-1, lvl+1) )); + + if(frontNode->right) + q.push(make_pair(frontNode->right, make_pair(hd+1, lvl+1))); + } + + for(auto i: nodes) { + + for(auto j:i.second) { + + for(auto k:j.second) + { + ans.push_back(k); + } + } + } + return ans; + } +}; + + + +// { Driver Code Starts. +int main() { + int t; + string tc; + getline(cin,tc); + t=stoi(tc); + while(t--) + { + string s; + getline(cin,s); + // string c; + // getline(cin,c); + Solution obj; + Node* root = buildTree(s); + + vector res = obj.verticalOrder(root); + for (int i : res) cout << i << " "; + cout << endl; + } + return 0; +} + + + // } Driver Code Ends \ No newline at end of file diff --git a/Lecture064 Tree Interview Questions Day2/zigzag.cpp b/Lecture064 Tree Interview Questions Day2/zigzag.cpp new file mode 100644 index 00000000..a3042854 --- /dev/null +++ b/Lecture064 Tree Interview Questions Day2/zigzag.cpp @@ -0,0 +1,179 @@ +// { Driver Code Starts +//Initial Template for C++ + + +#include +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; +} + + +// 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 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 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 +//User function Template for C++ +/*Structure of the node of the binary tree is as +struct Node { + int data; + Node *left; + Node *right; + + Node(int val) { + data = val; + left = right = NULL; + } +}; +*/ + +class Solution{ + public: + //Function to store the zig zag order traversal of tree in a list. + vector zigZagTraversal(Node* root) + { + vector result; + if(root == NULL) + return result; + + queue q; + q.push(root); + + bool leftToRight = true; + + while(!q.empty()) { + + int size = q.size(); + vector ans(size); + + //Level Process + for(int i=0; i data; + + if(frontNode->left) + q.push(frontNode -> left); + + if(frontNode->right) + q.push(frontNode -> right); + } + + //direction change karni h + leftToRight = !leftToRight; + + for(auto i: ans) { + result.push_back(i); + } + } + return result; + } +}; + +// { Driver Code Starts. + +/* Driver program to test size function*/ + + + +int main() { + + + int t; + scanf("%d ", &t); + while (t--) { + string s, ch; + getline(cin, s); + + Node* root = buildTree(s); + + vector ans; + Solution ob; + ans = ob.zigZagTraversal(root) ; + + for (int i = 0; i < ans.size(); i++) + cout << ans[i] << " "; + + cout << endl; + + } + return 0; +} + // } Driver Code Ends \ No newline at end of file