LeetCode 987. Vertical Order Traversal of a Binary Tree
原题链接在这里:https://leetcode.com/problems/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.
Example 1:
Input: [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]
Explanation:
Without loss of generality, we can assume the root node is at position (0, 0):
Then, the node with value 9 occurs at position (-1, -1);
The nodes with values 3 and 15 occur at positions (0, 0) and (0, -2);
The node with value 20 occurs at position (1, -1);
The node with value 7 occurs at position (2, -2).
Example 2:

Input: [1,2,3,4,5,6,7]
Output: [[4],[2],[1,5,6],[3],[7]]
Explanation:
The node with value 5 and the node with value 6 have the same position according to the given scheme.
However, in the report "[1,5,6]", the node value of 5 comes first since 5 is smaller than 6.
Note:
- The tree will have between 1 and
1000nodes. - Each node's value will be between
0and1000.
题解:
For vertical order, we need a HashMap to maintain key as column.
When there is same column, they should be put into same value collection.
Here is one more extra constraint, that is to for same column and same row, have smaller value come first.
Thus when coming out the HashMap, sort the values first based on row, then based on value.
Time Complexity: O(n + logn*loglogn). n is the number of nodes. Thus the longest list is the height of tree m = logn. sort takes O(mlogm).
Space: O(n).
AC Java:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> verticalTraversal(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null){
return res;
} HashMap<Integer, List<Pair>> hm = new HashMap<>();
LinkedList<Pair> que = new LinkedList<>();
que.add(new Pair(0, 0, root));
int min = 0;
int max = 0; while(!que.isEmpty()){
Pair cur = que.poll();
min = Math.min(min, cur.x);
max = Math.max(max, cur.x);
hm.putIfAbsent(cur.x, new ArrayList<>());
hm.get(cur.x).add(cur); if(cur.node.left != null){
que.add(new Pair(cur.x-1, cur.y-1, cur.node.left));
} if(cur.node.right != null){
que.add(new Pair(cur.x+1, cur.y-1, cur.node.right));
}
} for(int i = min; i<=max; i++){
List<Pair> list = hm.get(i);
Collections.sort(list, (a, b) -> a.y == b.y ? a.node.val-b.node.val : b.y-a.y); List<Integer> item = new ArrayList<>();
for(Pair p : list){
item.add(p.node.val);
} res.add(item);
} return res;
}
} class Pair{
int x;
int y;
TreeNode node;
public Pair(int x, int y, TreeNode node){
this.x = x;
this.y = y;
this.node = node;
} public String toString(){
return "" + this.x + "^" + this.y + "^" + this.node.val;
}
}
类似Binary Tree Vertical Order Traversal.
LeetCode 987. Vertical Order Traversal of a Binary Tree的更多相关文章
- 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【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 ...
- 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 posit ...
- [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, ...
- 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 ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- lower_case_table_names与表格名称大小写的问题
1 简介 在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小写敏感 ...
- 通过欧拉计划学Rust(第1~6题)
最近想学习Libra数字货币的MOVE语言,发现它是用Rust编写的,看来想准确理解MOVE的机制,还需要对Rust有深刻的理解,所以开始了Rust的快速入门学习. 看了一下网上有关Rust的介绍,都 ...
- 在Azure DevOps Server (TFS)中实现VUE项目的自动打包
概述 Vue.js(读音 /vjuː/, 类似于 view)是一个构建数据驱动的 web 界面的渐进式框架.由于它在数据绑定.页面展示和使用简单方面有很大的优势,逐渐被越来越多的前端开发团队使用.本文 ...
- 在Visual Studio中新增生成项目
在Visual Studio中新增生成项目 选择适配器类型 选择WCF-SQL适配器 创建连接选项 选择相应的存储过程 生成相应的消息架构
- Django+nginx+gunicore+supervisor+阿里云主机 部署博客项目
1 准备阶段 1 新鲜的阿里云主机 2 购买一个喜欢的域名 3 创建python的虚拟环境 2 阿里云主机的选取 咱们就是为了实验,我买了最便宜的阿里云主机 3 阿里云主机创建一个超级用户 3.1 默 ...
- centos 7 安装python3 & pip3
1.安装python3 https://www.cnblogs.com/Trees/p/7497482.html 2.解决:python ModuleNotFoundError: No module ...
- 文件上传之靶场upload-labs (11-20)
第十一关 strrpos() 函数查找字符串在另一字符串中最后一次出现的位置 substr() 函数返回字符串的一部分 文件保存的方式是上传路径+随机时间+截取的文件后缀 其中上传路径可控,可以利用这 ...
- ReentrantReadWriteLock可重入,锁升级,锁降级
public class ReentrantReadWriteLockTest { public static void main(String[] args) throws InterruptedE ...
- react的模型:react是如何工作的?
1.jsx:语法模型,语句构建模型: 2.组件:集合模型,组件管理: 3.vdom:分层模型.渲染管理模型: 4.flux:管道模型.数据模型,状态管理模型: 整体上是一个UI系统从上到下的构建: f ...
- 【JVM】jstat命令详解---JVM的统计监测工具
java进程的PID获取命令: https://www.cnblogs.com/sxdcgaq8080/p/10734752.html ================================ ...