原题链接在这里: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. SpringBoot第十三篇:日志处理

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10973583.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言   日志是软件 ...

  2. 【已解决】C#导入音频文件

    C#导入音频文件,找到Resources.resx,再进行导入. 直接添加文件到Resources是不能使用的. 解决的具体步骤: 引用音频文件核心代码: //播放背景音乐 SoundPlayer m ...

  3. CentOS中设置Apache服务器网站访问日志[每天的日志]

    在阿里云的linux 服务器下Apache的日志默认设置是七天更新一次, 并且所在的目录无法通过FTP浏览器查看, 这样让小白操作起来非常麻烦 可以使用rotatelogs来设置服务器的网站访问日志按 ...

  4. 【转帖】分布式事务之解决方案(XA和2PC)

    分布式事务之解决方案(XA和2PC) https://zhuanlan.zhihu.com/p/93459200 ​ 博彦信息技术有限公司 java工程师 3. 分布式事务解决方案之2PC(两阶段提交 ...

  5. Java for循环每次都通过list.size()和 string.length()获取大小是否消耗性能?

    前言 有人说在for循环之前用一个局部变量先获取到list.size().str.length(),然后在for循环的判断条件里通过这个局部变量替换list.size().str.length()会节 ...

  6. vue条件语句、循环语句、计算属性、侦听器监听属性

    因为 v-if 和v-for是一个指令,所以必须将它添加到一个元素上.但是如果想切换多个元素呢?此时可以把一个 <template> 元素当做不可见的包裹元素,并在上面使用 v-if.最终 ...

  7. 【leetcode-198】打家劫舍

    你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...

  8. SQL Server使用加密连接SSL/TLS (转载)

    说明 应用程序通过未加密的通道与数据库服务器通信, 这可能会造成重大的安全风险.在这种情况下, 攻击者可以修改用户输入的数据, 甚至对数据库服务器执行任意 SQL 命令.例如,当您使用以下连接字符串时 ...

  9. ASP.NET MVC中Log4Net记录错误日志的使用

    第一.在管理NuGet程序包 =>下载 Log4Net 第二.在web.config配置Log4Net 1:在<configuration>节点下 <configSection ...

  10. C# winform中组合键奇怪不响应问题

    再winform中使用ProcessCmdKey处理快捷键响应,针对单一快捷键响应没有任何问题.但是针对组合键总是无法响应,如下: protected override bool ProcessCmd ...