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 ...
随机推荐
- Kubernetes的核心技术概念和API对象
Kubernetes的核心技术概念和API对象 API对象是K8s集群中的管理操作单元.K8s集群系统每支持一项新功能,引入一项新技术,一定会新引入对应的API对象,支持对该功能的管理操作.例如副本集 ...
- 用python实现新词发现程序——基于凝固度和自由度
互联网时代,信息产生的数量和传递的速度非常快,语言文字也不断变化更新,新词层出不穷.一个好的新词发现程序对做NLP(自然预言处理)来说是非常重要的. N-Gram加词频 最原始的新词算法莫过于n-gr ...
- openssl/opensslv.h错误的解决方案
sudo apt install libssl-dev
- Const *ptr ptr
1. const int *ptr = NULL; <=> int const *ptr = NULL; 1) 表示指向符号常量的指针变量,指针变量本身并非const所以可以指向其他变量. ...
- BZOJ 3308 毒瘤结论 网络流
结论:在答案集合中的数其质因数最多有两个 且有两个的话一个>n1/2一个<n1/2 这样我们就可以把所有质数筛出来 弄成二分图 左边是<n1/2右边是>n1/2的 所以先把单个 ...
- mysql中FIND_IN_SET()和GROUP_CONCAT()的使用
知识点:mysql中FIND_IN_SET和GROUP_CONCAT()的使用 (一) 场景:当我们使用mysql数据库,查询一张的数据,其中的一列存放的是是另一张表id用“,”拼接的字符串 如下图所 ...
- Hadoop添加LZO压缩支持
启用lzo的压缩方式对于小规模集群是很有用处,压缩比率大概能降到原始日志大小的1/3.同时解压缩的速度也比较快. 安装 准备jar包 1)先下载lzo的jar项目https://github.com/ ...
- JDK源码那些事儿之常用的ArrayList
前面已经讲解集合中的HashMap并且也对其中使用的红黑树结构做了对应的说明,这次就来看下简单一些的另一个集合类,也是日常经常使用到的ArrayList,整体来说,算是比较好理解的集合了,一起来看下 ...
- Codeforces Educational Codeforces Round 54 题解
题目链接:https://codeforc.es/contest/1076 A. Minimizing the String 题意:给出一个字符串,最多删掉一个字母,输出操作后字典序最小的字符串. 题 ...
- [Python自学] day-22 (1) (Session、CSRF、中间件)
一.响应函数补充 三种返回响应的方式: return HttpResponse() return render() return redirect() HttpResponse: 除了能够返回字符串, ...