diff --git a/C++/Vertical_Treversal_Of_Binary_Tree.cpp b/C++/Vertical_Treversal_Of_Binary_Tree.cpp new file mode 100644 index 0000000..f7852d6 --- /dev/null +++ b/C++/Vertical_Treversal_Of_Binary_Tree.cpp @@ -0,0 +1,157 @@ +// { Driver Code Starts +#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; +} + +// 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) + { + //Your code here + vectorans; + map>>mp; // map stores the value of node present at vertical width x and level y + queue>>q; // node value, vertical length, level of tree stored in queue + q.push({root,{0,0}}); + while(!q.empty()){ + auto p = q.front(); + q.pop(); + Node* node = p.first; + int x = p.second.first; + int y = p.second.second; + mp[x][y].push_back(node->data); + if(node->left){ + q.push({node->left,{x-1,y+1}}); // when moves left side of the tree width decremented + } + if(node->right){ + q.push({node->right,{x+1,y+1}}); // when moves right side of the tree width incremented + } + } + for(auto m : mp){ + for(auto n : m.second){ + ans.insert(ans.end(),n.second.begin(),n.second.end()); + } + } + 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; +} +