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 ...
随机推荐
- 学习CSRF漏洞并挖掘CSRF漏洞
什么是跨站请求伪造? 跨站请求伪造(英语:Cross-siterequest forgery),也被称为one-clickattack或者session riding,通常缩写为CSRF或者XSRF, ...
- 用cef Python打造自己的浏览器
背景 项目需要做一个客户端的壳,内置浏览器,访问指定 的url 采用技术 python3.5 cefpython https://github.com/cztomczak/cefpython#inst ...
- IDEA光标跟随原因是jdk载入的问题BUG 修正 需要I安装最新版本1.8JDK 1.9之后的不清楚 谨慎
解决方法是 选择jdk本地版本 重启就好了 https://blog.csdn.net/Utopia_Zq/article/details/81190300?utm_source=blogxgwz2 ...
- 自定义printf 打印函数
文章转载自:https://blog.csdn.net/varding/article/details/8109440 我们在程序里经常需要打印一些字符串来调试或者显示,最简单的方法: char ...
- 2.Vue调试工具vue-devtools的安装步骤和使用
1.安装步骤: 打开谷歌浏览器设置 -->扩展程序 -->勾选开发者模式 --->加载已解压的扩展程序 --->选择“chrome扩展”文件夹即可:
- ubuntu python3虚拟环境
mkvirtualenv flow_chart -p /usr/bin/python3.6 # 命令 环境名 -p python所在路径 pip install -r request ...
- Why do we name variables in Tensorflow?
Reference:Stack Overflow. The name parameter is optional (you can create variables and constants wit ...
- gitlab-ce白名单设置杜绝并发数过大引起的封ip故障
gitlab-ce 7.9安装手札以及上篇文章的问题解决 鸣谢 感谢ruby大神===>章鱼的一路指点,才能拨开迷雾见云天! 章鱼大人: 国内Ansible部落原创翻译之一! 资深运维! ROR ...
- 关于静态资源放在CDN上
https://www.netlify.com/ https://app.netlify.com/signup?_ga=2.194141613.1097457726.1543799087-101005 ...
- tkinter_战队数据查询系统
# 导入tkinter模块 import tkinter from tkinter import ttk # 导入库 import pymysql # 创建主窗口对象 root = tkinter.T ...