Rishav_hitk_cse

Untitled

Jan 21st, 2021
457
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vector<vector<int> > Solution::verticalOrderTraversal(TreeNode* root) {
  2.     unordered_map<int,vector<int>> mp;
  3.     int mn = 0,mx = 0;
  4.     queue<pair<TreeNode*,int>> q;
  5.     if(!root) return {};
  6.     q.push({root,0});
  7.     while(q.size()){
  8.         int sz = q.size();
  9.         for(int i=0;i<sz;i++){
  10.             auto node = q.front();
  11.             q.pop();
  12.             mn = min(mn,node.second);
  13.             mx = max(mx,node.second);
  14.             mp[node.second].push_back(node.first->val);
  15.             if(node.first->left) q.push({node.first->left,node.second-1});
  16.             if(node.first->right) q.push({node.first->right,node.second+1});
  17.         }
  18.     }
  19.     vector<vector<int>> ans;
  20.     for(int i=mn;i<=mx;i++)
  21.         ans.emplace_back(mp[i]);
  22.     return ans;
  23. }
RAW Paste Data