LeetCode "Binary Tree Vertical Order"
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, ));
}
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 - ));
}
if (tmp->right)
{
q.push(Rec(tmp->right, l + ));
}
} vector<vector<int>> ret;
for (int i = minl; i <= maxl; i++)
{
if (hm[i].size() == ) continue;
ret.push_back(hm[i]);
} return ret;
}
};
LeetCode "Binary Tree Vertical Order"的更多相关文章
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
随机推荐
- CentOS6编译装载nbd模块
今天突然发现CentOS系统没有nbd模块,只能重新装下,下面记录下整个编译过程: 系统:CentOS6.5 内核:2.6.32-431.el6.x86_64 [root@localhost ~]# ...
- 大数据hadoop入门学习之集群环境搭建集合
目录: 1.基本工作准备 1.虚拟机准备 2.java 虚拟机-jdk环境配置 3.ssh无密码登录 2.hadoop的安装与配置 3.hbase安装与配置(集成安装zookeeper) 4.zook ...
- iOS学习笔记---oc语言第四天
字符串 数组 一.使用苹果帮助文档 学会使⽤用苹果帮助⽂文档是开发者的⼀一项技能 Inherits from 继承⾃自 Conforms to 遵循什么协议 Framework 属于哪个框架 Avai ...
- 十日谈 (share)
@拔赤 一直想写这篇“十日谈”,聊聊我对Web前端开发的体会,顺便解答下周围不少人的困惑和迷惘.我不打算聊太多技术,我想,通过技术的历练,得到的反思应当更重要. 我一直认为自己是“初级”前端开发工程师 ...
- int除以int 得到double类型值
double serviceability =(double)(count1+count2)/sum; 需要进行强转 除数 这样得到的值就是double类型了
- HDU 3555 数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- ZOJ 1016 Parencodings
原题链接 题目大意:有两串数字P和W.数组P中,数字P[i]表示第i个右括号之前的左括号个数.数组W中,数字W[i]表示在第i个右括号和与它匹配的左括号之间的右括号的个数(包括本身).给出一个数组P, ...
- C++ Primer : 第十二章 : 文本查询程序
C++ Primer书上这个例子讲的很不错,写写帮助自己理解标准库和智能指针. .h 文件内容 #include <fstream> #include <iostream> # ...
- mybatis+spring的简单介绍学习
参考下面链接 http://mybatis.github.io/spring/zh/index.html
- JavaWeb学习记录(十七)——JSP九大隐式对象
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOE ...