原题链接在这里: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:

  1. The tree will have between 1 and 1000 nodes.
  2. Each node's value will be between 0 and 1000.

题解:

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的更多相关文章

  1. 【LeetCode】987. Vertical Order Traversal of a Binary Tree 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  2. 【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 ...

  3. 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 ...

  4. [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 ...

  5. LeetCode Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  6. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  7. Binary Tree Vertical Order Traversal -- LeetCode

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  8. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  9. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

随机推荐

  1. redis对象存储(适用于订单系统自动更新)

    启动:redis-server.exe redis.windows.conf连接:redis-cli.exe -h 127.0.0.1 -p 6379 #插入取消的订单列表与时间: redis 127 ...

  2. Java并发之原子操作类汇总

    当程序更新一个变量时,如果是多线程同时更新这个变量,可能得到的结果与期望值不同.比如:有一个变量i,A线程执行i+1,B线程也执行i+1,经过两个线程的操作后,变量i的值可能不是期望的3,而是2.这是 ...

  3. Java连载13-整数型字面值的强制转换

    一.注意点 1.大容量不能直接赋值给小容量:大容量转化为小容量需要进行,强制类型转换,强制类型转换需要加上“强制类型转换符”,加上强制类型转换符之后编译通过了但是精度会有有可能损失.所以强制类型转换要 ...

  4. [转帖]PostgreSQL的时间/日期函数使用

    PostgreSQL的时间/日期函数使用 https://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html 这个博客的 文章目录比上一个好十 ...

  5. Reflector调试dll功能

    Reflector不仅仅是一个反编译工具,之前用Resharper,把这个给忽略了,这个Reflector还有一个调试dll功能, 在调试时反编译代码,会生成对应的pdb文件,就可以进行dll源码调试 ...

  6. Jmeter参数化之数据库读取数据

    以读取mysql数据库为例 1.下载一个mysql驱动包,最好去mysql官网下载 下载网址:https://dev.mysql.com/downloads/connector/j/ Select O ...

  7. CSS实现水平垂直居中的数种方法整合

    CSS实现水平垂直居中可以说是前端老生常谈的问题了,一般面试官问的时候面试者都会回答出来,但是继续追问还有没有其他方法的时候有可能就说不出来了. 本着学习知识的目的,特在此纪录CSS实现水平垂直居中的 ...

  8. Linq分批次,每组1000条

    /// <summary> /// 分组插入每次插入1000 /// </summary> /// <param name="data">< ...

  9. NET/Regex 处理连续空格

    问题: 就是一个字符串呀,一个字符串,里面的话有一个空格,有可能有连续空格,你遇到连续空格,把这个连续空格变成一个空格,一个空格地不处理. 代码: /// <summary> /// 处理 ...

  10. 经典SQL语句使用方法大全(自留用)

    一.基础 1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份 ...