Trick: use a pair map
class Solution {
public:
vector<vector<int>> verticalOrder(TreeNode* root) {
if(!root) return {};
vector<vector<int>> res;
map<int, vector<int>> r;
queue<pair<int, TreeNode*>> q;
q.push(make_pair(0, root));
while(!q.empty()){
auto t = q.front(); q.pop();
int f = t.first;
TreeNode* tree = t.second;
r[f].push_back(tree->val);
if(tree->left) q.push(make_pair(f-1, tree->left));
if(tree->right) q.push(make_pair(f+1, tree->right));
}
for(auto a:r){
res.push_back(a.second);
}
return res;
}
};