LeetCode "Binary Tree Vertical Order"
2015-12-15 12:26
399 查看
BFS + HashTable
class Solution { int maxl, minl; unordered_map<int, vector<int>> hm; public: vector<vector<int>> verticalOrder(TreeNode* root) { maxl = INT_MIN; minl = INT_MAX; typedef pair<TreeNode*, int> Rec; queue<Rec> 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<vector<int>> ret; for (int i = minl; i <= maxl; i++) { if (hm[i].size() == 0) continue; ret.push_back(hm[i]); } return ret; } };
相关文章推荐
- R树空间索引
- Delphi属性Property学习应用总结
- Impala与Hbase整合用于ETL过程尝试(1)
- Android里面主菜单里面设置菜单列表,单项选择,多项选择的实现
- 采用大杀招QEMU调试Linux内核代码
- 服务器相关名词解释
- 300. Longest Increasing Subsequence
- C#下载文件(TransmitFile/WriteFile/流方式)实例介绍
- HDOJ--2002
- win32记事本程序(一)
- HDU 2845 dp压缩元素 借助递推
- Jmeter参数化的4种方法
- 中级篇——背包问题1(01背包)
- OC NSArray数组排序
- Json详解
- JavaScript获取当前cpu使用率的方法
- UI-9-UITableView
- CSS里的各种水平垂直居中
- IntelliJ IDEA常用快捷键整理
- 常识