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

If two nodes are in the same row and column, the order should be from left to right.

Examples 1:

Input: [3,9,20,null,null,15,7]

3
/\
/ \
9 20
/\
/ \
15 7 Output: [
[9],
[3,15],
[20],
[7]
]

Examples 2:

Input: [3,9,8,4,0,1,7]

     3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7 Output: [
[4],
[9],
[3,0,1],
[8],
[7]
]

Examples 3:

Input: [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5)

     3
/\
/ \
9 8
/\ /\
/ \/ \
4 01 7
/\
/ \
5 2 Output: [
[4],
[9,5],
[3,0,1],
[8,2],
[7]
]

这道题让我们竖直遍历二叉树,并把每一列存入一个二维数组,看题目中给的第一个例子,3和 15 属于同一列,3在前,第二个例子中,3,5,2 在同一列,3在前,5和2紧随其后,那么隐约的可以感觉到好像是一种层序遍历的前后顺序,如何来确定列的顺序呢,这里可以把根节点给个序号0,然后开始层序遍历,凡是左子节点则序号减1,右子节点序号加1,这样可以通过序号来把相同列的节点值放到一起,用一个 TreeMap 来建立序号和其对应的节点值的映射,用 TreeMap 的另一个好处是其自动排序功能可以让列从左到右,由于层序遍历需要用到 queue,此时 queue 里不能只存节点,而是要存序号和节点组成的 pair 对儿,这样每次取出就可以操作序号,而且排入队中的节点也赋上其正确的序号,代码如下:

class Solution {
public:
vector<vector<int>> verticalOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
map<int, vector<int>> m;
queue<pair<int, TreeNode*>> q;
q.push({, root});
while (!q.empty()) {
auto a = q.front(); q.pop();
m[a.first].push_back(a.second->val);
if (a.second->left) q.push({a.first - , a.second->left});
if (a.second->right) q.push({a.first + , a.second->right});
}
for (auto a : m) {
res.push_back(a.second);
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/314

类似题目:

Binary Tree Level Order Traversal

参考资料:

https://leetcode.com/problems/binary-tree-vertical-order-traversal/

https://leetcode.com/problems/binary-tree-vertical-order-traversal/discuss/76401/5ms-Java-Clean-Solution

https://leetcode.com/problems/binary-tree-vertical-order-traversal/discuss/76508/3ms-java-solution-beats-100

https://leetcode.com/problems/binary-tree-vertical-order-traversal/discuss/76463/Using-HashMapBFS-Java-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历的更多相关文章

  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] 314. Binary Tree Vertical Order Traversal 二叉树的垂直遍历

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

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

  4. LeetCode 314. Binary Tree Vertical Order Traversal

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

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

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

  6. 314. Binary Tree Vertical Order Traversal

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

  7. [LC] 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 (二叉树的层序遍历)

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

  9. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

随机推荐

  1. Docker系列之学习笔记

    一.Docker简介 1.1.Docker架构 Docker 使用客户端-服务器 (C/S) 架构模式,分为Docker守护进程和客户端,Docker 客户端,实际上是 docker 的二进制程序,D ...

  2. CodeForce 359C Prime Number

    Prime Number CodeForces - 359C Simon has a prime number x and an array of non-negative integers a1,  ...

  3. 转 推荐 33 个 IDEA 最牛配置,写代码太爽了!

    本文由 简悦 SimpRead 转码, 原文地址 https://mp.weixin.qq.com/s/neyvJouuG1Rmxn3BwfRXVg 作者:琦彦 blog.csdn.net/fly91 ...

  4. 排障利器之远程调试与监控 --jmx & remote debug

    监控和调试功能是应用必备的属性之一,其手段也是多种多样. 一般地,我们可以通过:线上日志, zabbix, grafana, cat 等待系统做一问题留底,有问题及时报警,从而达到监控效果. 而对于应 ...

  5. U9创建BE组件

    打开UBF,新建项目->实体项目 输入名称后,点击确定,第二步:修改名称以在后期作为文件夹区分 第三步:创建实体 第四步:添加U9基础对象引用 拖动到解决方案的Reference 第五步:右键构 ...

  6. C 补充

    a++与++a的区别 #include <stdio.h> void main(){ int a=5,b=5; int i=0; for(i=0;i<2;i++){ printf(& ...

  7. 算法笔记 第6章 C++标准模版库(STL)介绍 学习笔记

    6.1 vector的常见用法详解 vector:变长数组,长度根据需要而自动改变的数组 要使用vector,则需要添加vector头文件,即#include<vector>,还需要在头文 ...

  8. MySQL 8.0.18安装教程(windows 64位)

    目录 1-先去官网下载点击的MySQL的下载​ 2-配置初始化的my.ini文件的文件3-初始化MySQL4-安装MySQL服务 + 启动MySQL 服务5-连接MySQL + 修改密码 * 第一项 ...

  9. Vim操作:打开文件

    1.打开文件并定位到某一行 vim +20 vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php # 定位至第20行 2 ...

  10. 关于创建node服务

    1.环境条件准备: A.确定node已经创建 B.npm或cnpm已经下载,npm和cnpm其实是一个道理 C.mysql或者使用其他数据库已经安装(本例使用mysql) 2.开始创建,首先新建一个文 ...