Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions C++/Vertical_Treversal_Of_Binary_Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// { Driver Code Starts
#include <bits/stdc++.h>
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<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;
}

// Function for Inorder Traversal
void printInorder(Node* root)
{
if(!root)
return;

printInorder(root->left);
cout<<root->data<<" ";
printInorder(root->right);
}


// } Driver Code Ends
class Solution
{
public:
//Function to find the vertical order traversal of Binary Tree.
vector<int> verticalOrder(Node *root)
{
//Your code here
vector<int>ans;
map<int,map<int,vector<int>>>mp; // map stores the value of node present at vertical width x and level y
queue<pair<Node*,pair<int,int>>>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 <int> res = obj.verticalOrder(root);
for (int i : res) cout << i << " ";
cout << endl;
}
return 0;
}