diff --git a/algorithms/CPlusPlus/Trees/Vertical_Order.cpp b/algorithms/CPlusPlus/Trees/Vertical_Order.cpp deleted file mode 100644 index e68befdb..00000000 --- a/algorithms/CPlusPlus/Trees/Vertical_Order.cpp +++ /dev/null @@ -1,43 +0,0 @@ -class Solution -{ - public: - //Function to find the vertical order traversal of Binary Tree. - vector verticalOrder(Node *root) - { - //Your code here - list>l;//queue having hd and node - map>mp;// a vertical level mapped to ele in that level - vectorans;//ans vector - if(!root)return ans; - l.push_back({0,root}); - while(!l.empty()){ - - pairp=l.front(); - //frst is hd and second is node - mp[p.first].push_back(p.second->data);//map data to the vertical level it belongs to - if(p.second->left)l.push_back({p.first-1,p.second->left});//hd=current-1 for left - if(p.second->right)l.push_back({p.first+1,p.second->right});//hd=current +1 for right - l.pop_front(); - } - // now map has elements acc to level - // make a vector each for a level - //push that vector to ans vector - - /* map looks like this - frst snd - begin: (-1)-> [9] - begin+1: (0)-> [3,15] - begin+2: (1)-> [20] - begin+3: (2)-> [7] */ - for(auto it=mp.begin();it!=mp.end();it++){ - pair>q=*it; - int size=q.second.size(); - vectorlevel; - for(int i=0;i +using namespace std; + + +// Basic Structure of a Tree +struct Node { + int data; + struct Node *left; + struct Node *right; + + Node(int val) { + data = val; + left = NULL; + right = NULL; + } +}; + + //Function to find the vertical order traversal of Binary Tree. +vector verticalOrder(Node *root) +{ + list>l;//queue having hd and node + map>mp;// a vertical level mapped to ele in that level + vectorans;//ans vector + if(!root)return ans; + l.push_back({0,root}); + while(!l.empty()){ + + pairp=l.front(); + //frst is hd and second is node + mp[p.first].push_back(p.second->data);//map data to the vertical level it belongs to + if(p.second->left)l.push_back({p.first-1,p.second->left});//hd=current-1 for left + if(p.second->right)l.push_back({p.first+1,p.second->right});//hd=current +1 for right + l.pop_front(); + } + // now map has elements acc to level + // make a vector each for a level + //push that vector to ans vector + + /* map looks like this + frst snd + begin: (-1)-> [9] + begin+1: (0)-> [3,15] + begin+2: (1)-> [20] + begin+3: (2)-> [7] */ + for(auto it=mp.begin();it!=mp.end();it++){ + pair>q=*it; + int size=q.second.size(); + vectorlevel; + for(int i=0;ileft = new Node(2); + root->right = new Node(3); + root->left->left = new Node(4); + root->left->right = new Node(5); + root->right->left = new Node(6); + root->right->right = new Node(7); + root->right->left->right = new Node(8); + root->right->right->right = new Node(9); + + vectorans; + ans=verticalOrder(root); + + for(int i:ans){ + cout<