LC 987. Vertical Order Traversal of a Binary Tree
Given a binary tree, return the vertical order traversal of its nodes values.
For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).
Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).
If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.
class Solution {
public:
vector<vector<int>> verticalTraversal(TreeNode* root) {
map<int,map<int,vector<int>>> mp;
vector<vector<int>> ret;
helper(root, , , mp);
for(auto it=mp.begin(); it!=mp.end(); it++){
vector<int> a;
for(auto iit=it->second.rbegin(); iit!=it->second.rend(); iit++) {
vector<int> tmp = iit->second;
sort(tmp.begin(), tmp.end());
for(auto x : tmp) a.push_back(x);
}
ret.push_back(a);
}
return ret;
}
void helper(TreeNode* root,int x,int y, map<int,map<int,vector<int>>>& mp){
if(!root) return;
mp[x][y].push_back(root->val);
helper(root->left, x-, y-, mp);
helper(root->right, x+, y-, mp);
}
};
LC 987. Vertical Order Traversal of a Binary Tree的更多相关文章
- LeetCode 987. Vertical Order Traversal of a Binary Tree
原题链接在这里:https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/ 题目: Given a binary ...
- 【leetcode】987. Vertical Order Traversal of a Binary Tree
题目如下: Given a binary tree, return the vertical order traversal of its nodes values. For each node at ...
- 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [Swift]LeetCode987. 二叉树的垂序遍历 | Vertical Order Traversal of a Binary Tree
Given a binary tree, return the vertical order traversal of its nodes values. For each node at posit ...
- LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- LeetCode 314. Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...
- Binary Tree Vertical Order Traversal -- LeetCode
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [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 ...
随机推荐
- EditText编辑框
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- openpose-opencv 的coco数据多人体姿态估计
介绍 opencv除了支持常用的物体检测模型和分类模型之外,还支持openpose模型,同样是线下训练和线上调用.这里不做特别多的介绍,先把源代码和数据放出来- 实验模型获取地址:https://gi ...
- SQL SERVER 2008 存储过程传表参数
最近项目使用到了存储过程传入表类型参数. --定义表类型 create type t_table_type as table ( id int, name varchar(32), sex var ...
- MVVM框架-MVVMLight
项目URL:http://www.mvvmlight.net/ 一.安装MVVMLight 在NuGet程序包中搜索MVVMLight,然后安装. 二.使用 安装完MVVMLight后项目中会自动生成 ...
- python画图matplolib
http://python.jobbole.com/85106/ 1.画二维图 2.画三维图 我的电脑只能在jupyter notebook上面运行才能看的到,常规import库 %matplotli ...
- C语言I 博客作业03
这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 作业要求 我在这个课程的目标是 掌握关系运算.if-else语句.格式化输入语句scanf(),以及常用的数学库函数 这个作业在那个具体方 ...
- Python 练习实例2
Python 练习实例2 题目:企业发放的奖金根据利润提成.利润(I)低https://www.xuanhe.net/于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元 ...
- jquery的tap会执行2次的替换办法
用touchend替换 $(".videoCall").on("touchend",function(){ })$(".videoCall" ...
- centos7 配置静态ip时出现双ip问题解决
1.先用ifconfig,看看有几个网卡 2.执行vi /etc/sysconfig/network-scripts/ifcfg-eth0最后的网卡名字改为实际的 3.静态ip只需要设置着几个地方,B ...
- tarjan——校园网(缩点,再构图)
P2746 [USACO5.3]校园网Network of Schools 任务一:求缩完点后入度为0的点的个数(有向边) 任务二:求缩完点后入度为0和出度为0的最大值(要把图构造成强连通分量) 注意 ...