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. MySQL的Explain解释器的部分理解

    Explain 部分说明进行解释 (1) Extra列的Using Where 表示在进行过滤后在进行Where语句的过滤 (2) type为ref,直接按索引顺序返回,没有 Using fileso ...

  2. ucsc工具集合

    wget http://hgdownload.cse.ucsc.edu/admin/jksrc.zip 解压后所有工具在kent目录里 发现个更方便的.... http://hgdownload.cs ...

  3. .net中创建xml文件

    //创建空的XML文档 XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml("<?xml version='1.0' enco ...

  4. Codeforces 106 C 多重背包

    题目链接:http://codeforces.com/problemset/problem/106/C 根据题意列出式子,设每种蛋糕做了xi个,则对于每种材料bi*xi<=ai. 对于dough ...

  5. makefile--模式规则(七)

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节讲到目录创建成功,目标文件没有生产到对应目录下,这里我们先给目标文件加上对应目录,这样的话 ...

  6. pthread_join和pthread_detach的用法(转)

    一:关于join join join是三种同步线程的方式之一.另外两种分别是互斥锁(mutex)和条件变量(condition variable). 调用pthread_join()将阻塞自己,一直到 ...

  7. hadoop删除节点

    1.修改Master节点Hdfs-site.xml,增加dfs.hosts.exclude参数 eg: <property> <name>dfs.hosts.exclude&l ...

  8. spark-scheduled调度算法

    1.3源码是如此,后面新版本源码会尽可能的根据用户shell配置的参数进行分配 1.spareadOutApps 尽可能分配到多的机器上面execute和CPU core 2.非spareadouta ...

  9. DuiLib——xml配置项

    DUILibxml配置项 根节点 子类 属性 类型 Window  |--------Image 图片信息 | o------name string | o------restype string | ...

  10. MySQL 常用show命令

    MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...