BFS + HashTable
class Solution { int maxl, minl; unordered_map
> hm;public: vector
> verticalOrder(TreeNode* root) { maxl = INT_MIN; minl = INT_MAX; typedef pair Rec; queue q; if (root) { q.push(Rec(root, 0)); } while (!q.empty()) { Rec curr = q.front(); q.pop(); int l = curr.second; maxl = max(maxl, l); minl = min(minl, l); TreeNode *tmp = curr.first; hm[l].push_back(tmp->val); if (tmp->left) { q.push(Rec(tmp->left, l - 1)); } if (tmp->right) { q.push(Rec(tmp->right, l + 1)); } } vector > ret; for (int i = minl; i <= maxl; i++) { if (hm[i].size() == 0) continue; ret.push_back(hm[i]); } return ret; }};