[LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
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/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历的更多相关文章
- [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 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode 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]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: 102_Binary Tree Level Order Traversal | 二叉树自顶向下的层次遍历 | Easy
题目:Binay Tree Level Order Traversal Given a binary tree, return the level order traversal of its nod ...
- [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] 102. 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 ...
随机推荐
- 多项目并行开发如何做到快速切换——sublime Text3
sublime text有一个很人性化的功能,就是打开窗口的时候,它会把上一次关闭时的编辑器工作区状态完全复原(不论文件是否已经保存). 只有一个项目的时候,这个功能非常方便,可以保证重启电脑后cod ...
- JavaScript易错知识点整理
前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函数参数,闭包问题及对象拷贝与赋值这6个方面进行由浅入深的介绍和讲解,其中也涉及了一 ...
- Visual Studio (VSIX,项目模板 )制作
下载Vsiual Studio 2012 SDK 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=30668 提示:一定要注意 ...
- asp.net DataTable导出Excel 自定义列名
1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...
- 【无私分享:ASP.NET CORE 项目实战(第四章)】Code First 创建数据库和数据表
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 本章我们来介绍下Asp.net Core 使用 CodeFirst 创建数据库和表,通过 控制台 和 dotnet ef 两种 ...
- C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试
在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...
- Atitit.ide技术原理与实践attilax总结
Atitit.ide技术原理与实践attilax总结 1.1. 语法着色1 1.2. 智能提示1 1.3. 类成员outline..func list1 1.4. 类型推导(type inferenc ...
- intellij中不能导入jar包
①选中项目点击右键,选择open modual settings(或者直接按F4): ②在弹出的窗口左端选择Libraries: ③点击左边加号,选择From Maven Repository; ④将 ...
- JDBC关于时间的存取
Oracle数据库默认时间存储是java.sql.date,而java程序中的时间默认是java.util.date,所以通过JDBC存取的 时候会涉及到时间的转换问题. 1.日期存取 存入数据库的时 ...
- javaScript之BOM操作2
<!doctype html> <html lang="en"> <head> <title>Document</title& ...