Striver-SDE-Sheet

Repository containing solution for #SdeSheetChallenge by striver

View the Project on GitHub

Vertical order traversal

Solution

Code

class Solution
{
    public:
    //Function to find the vertical order traversal of Binary Tree.
    vector<int> verticalOrder(Node *root)
    {
        vector<int> ans;
        if(root==NULL)
            return ans;
        map<int,vector<int>> mp;
        queue<pair<Node*,int>> q;
        // node, horizontal distance
        q.push({root,0});
        while(!q.empty())
        {
            pair<Node*,int> tp=q.front();
            q.pop();
            mp[tp.second].push_back(tp.first->data);
            if(tp.first->left)
                q.push({tp.first->left,tp.second-1});
            if(tp.first->right)
                q.push({tp.first->right,tp.second+1});
        }
        for(auto it : mp)
        {
            for(auto it1 : it.second)
                ans.push_back(it1);
        }
        return ans;
    }
};