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, ...
随机推荐
- com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错
-- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...
- 用lucene4.10.2分词器进行分词
import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.luc ...
- Azure SQL 数据库弹性池现已面市
我们高兴地宣布Azure SQL 数据库弹性池现已正式面市.弹性池自去年试运行以来,得到许多软件即服务(SaaS)供应商和企业开发人员的认可,他们利用弹性池管理持续增长的云数据库和应用程序,成果高效. ...
- Notes of Linked Data concept and application - TODO
Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...
- python 函数基础
定义: def intersect(seq1,seq2): res = [] for x in seq1: if x in seq2: res.append(x) yield res 运行效果 > ...
- JS开发者常用的10个Sublime Text插件
Sublime Text 是每个开发者工具箱中都应该有的一个强大的应用.它是一个跨平台的.高定制化的.高级的文本编辑器,在功能强大的 集成开发环境(众所周知地消耗资源)和类似于 Vim 或 Emacs ...
- leetcode 96 Unique Binary Search Trees ----- java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LAMT基于mod_jk方式的负载均衡集群
一.系统环境 1.apache服务器 系统环境:CentOS release 6.5 (Final) ip地址:192.168.1.203 2.tomcat1服务器 系统环境:CentOS relea ...
- poj2367 拓扑序
题意:有一些人他们关系非常复杂,一个人可能有很多后代,现在要制定一种顺序,按照前辈在后代前排列就行 拓扑序裸题,直接建边拓扑排序一下就行了. #include<stdio.h> #incl ...
- Android 编程下去除 ListView 上下边界蓝色或黄色阴影
默认的情况下,在 ListView 滑动到顶部或者是底部的时候,会有黄色或者蓝色的阴影出现.在不同的版本上解决的方法是不同的,在 2.3 版本之前可以在 ListView 的属性中通过设置 andro ...