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"的更多相关文章

  1. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  2. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  3. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  6. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  7. [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 ...

  8. [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 ...

  9. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

随机推荐

  1. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 数据库报错

    -- 查询mysql 数据库链接空闲时间持有最大空闲时间,单位为秒 SHOW VARIABLES WHERE VAriable_name = 'interactive_timeout'; -- 会出现 ...

  2. 用lucene4.10.2分词器进行分词

    import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.luc ...

  3. Azure SQL 数据库弹性池现已面市

    我们高兴地宣布Azure SQL 数据库弹性池现已正式面市.弹性池自去年试运行以来,得到许多软件即服务(SaaS)供应商和企业开发人员的认可,他们利用弹性池管理持续增长的云数据库和应用程序,成果高效. ...

  4. Notes of Linked Data concept and application - TODO

    Motivation [反正债多了不愁,再开个方向.] Data plays a core role in most business systems, data storage and retrie ...

  5. python 函数基础

    定义: def intersect(seq1,seq2): res = [] for x in seq1: if x in seq2: res.append(x) yield res 运行效果 > ...

  6. JS开发者常用的10个Sublime Text插件

    Sublime Text 是每个开发者工具箱中都应该有的一个强大的应用.它是一个跨平台的.高定制化的.高级的文本编辑器,在功能强大的 集成开发环境(众所周知地消耗资源)和类似于 Vim 或 Emacs ...

  7. 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 ...

  8. LAMT基于mod_jk方式的负载均衡集群

    一.系统环境 1.apache服务器 系统环境:CentOS release 6.5 (Final) ip地址:192.168.1.203 2.tomcat1服务器 系统环境:CentOS relea ...

  9. poj2367 拓扑序

    题意:有一些人他们关系非常复杂,一个人可能有很多后代,现在要制定一种顺序,按照前辈在后代前排列就行 拓扑序裸题,直接建边拓扑排序一下就行了. #include<stdio.h> #incl ...

  10. Android 编程下去除 ListView 上下边界蓝色或黄色阴影

    默认的情况下,在 ListView 滑动到顶部或者是底部的时候,会有黄色或者蓝色的阴影出现.在不同的版本上解决的方法是不同的,在 2.3 版本之前可以在 ListView 的属性中通过设置 andro ...