[LeetCode] 314. 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] 314. Binary Tree Vertical Order Traversal 二叉树的竖直遍历的更多相关文章
- [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] 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 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [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, ...
- 314. Binary Tree Vertical Order Traversal
题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...
- [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 ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 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, ...
随机推荐
- pixijs shader教程
pixijs 写shader 底层都封装好了 只要改改片段着色器就行了 pxijs一定刚要设置支持透明 不然 颜色不支持透明度了 const app = new PIXI.Application({ ...
- h5中的audio音频标签与对应的dom对象
HTML5中提供了一个<audio>标签,同时也提供了一个Audio对象给JavaScript去操作,这个对象属于HTML的DOM对象. H5中的<audio>标签 此标签是H ...
- C#,二分法,BinarySearch()
static int BinarySearch(int[] arr,int key,int low,int high) { low = 0;high = arr.Length - 1; while(l ...
- 永久清理git中的历史大文件
原文发布于:https://www.chenxublog.com/2019/05/26/remove-git-big-files.html 有写老的git仓库,因为当年的无知,不会用.gitignor ...
- Visual Studio中的主题定制变得更加容易
有时Visual Studio的默认主题是不够的.幸运的是,我们刚刚重新设计了创建和导入自定义主题的过程. 导入主题的唯一方法之一是下载旧的Color Theme Editor扩展.如果你足够勇敢地创 ...
- js数组试列题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Java基础回顾——反射+案例
一,引言: 从大二刚开始接触java,了解其基本语法,到用swing编写图形界面的程序,到JavaWeb用SSH三大框架编写小网站,后面又学SSM,SpringBoot,SpringCloud等.可以 ...
- CTF必备技能丨Linux Pwn入门教程——ShellCode
这是一套Linux Pwn入门教程系列,作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的一些题目和文章整理出一份相对完整的Linux Pwn教程. 课程回顾>> Linu ...
- SPC软控件提供商NWA的产品在各行业的应用(化工行业)
Northwest Analytical (NWA)是全球领先的“工业4.0”制造分析SPC软件控件提供商.产品(包含: NWA Quality Analyst , NWA Focus EMI 和 N ...
- vue-v-for
1.v-for遍历数组和对象 <ul> <li v-for="item in array">{{item}}</li><br> &l ...