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

If two nodes are in the same row and column, the order should be from left to right.

Examples 1:

Input: [3,9,20,null,null,15,7]

3
/\
/ \
9 20
/\
/ \
15 7 Output: [
[9],
[3,15],
[20],
[7]
]

思路

代码

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
// corner
List<List<Integer>> results = new ArrayList<>();
if (root == null) return results;
// init
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Map<Integer, List<Integer>> map = new HashMap<>();
Queue<Position> queue = new LinkedList<>(); //use queue to bfs
queue.add(new Position(root, 0));
while (!queue.isEmpty()) {
Position position = queue.remove();
min = Math.min(min, position.column);
max = Math.max(max, position.column);
List<Integer> list = map.get(position.column);
if (list == null) {
list = new ArrayList<>();
map.put(position.column, list);
}
list.add(position.node.val);
if (position.node.left != null) queue.add(new Position(position.node.left, position.column-1));
if (position.node.right != null) queue.add(new Position(position.node.right, position.column+1));
}
for(int i = min; i<= max; i++) {
List<Integer> list = map.get(i);
if (list != null) results.add(list);
}
return results;
}
} class Position {
TreeNode node;
int column;
Position(TreeNode node, int column) {
this.node = node;
this.column = column;
}
}

[leetcode]314. Binary Tree Vertical Order Traversal二叉树垂直遍历的更多相关文章

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

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

  2. [LeetCode] 314. Binary Tree Vertical Order Traversal 二叉树的垂直遍历

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

  3. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. LeetCode 314. Binary Tree Vertical Order Traversal

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

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

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

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

  7. 314. Binary Tree Vertical Order Traversal

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

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

  9. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

随机推荐

  1. iOS 一些常用代码的总结

    一.运算符号前后都需要加空格 二.控件view都有initWithFrame 三.initWithSubview 和 layoutSubviews initWithSubview:初始化子控件 lay ...

  2. 链接mysql的两种方法

    使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@host]# my ...

  3. python读取文件中的字典

    import ast def file_read():    with open('D:\\pytharm\\jichuyufa\\day2\\pro_cty_con.txt', 'r', encod ...

  4. SQL Server占用服务器内存过高

    SQL Server对服务器内存的使用策略是用多少内存就占用多少内存,只用在服务器内存不足时,才会释放一点占用的内存,所以SQL Server 服务器内存往往会占用很高. 查看内存状态: DBCC M ...

  5. 进行web开发时应该考虑的架构性因素

    功能实现 这个自不必说. 性能与可伸缩性 根据预期的访问量,评估机器负载情况.如果在可预期的未来一台服务器可以撑得住,则没必要使用多台服务器.需要对多个环节进行性能评估:web服务器.逻辑服务器.DB ...

  6. 数据库入门4 结构化查询语言SQL

    知识内容: 1.了解SQL 2.库.表操作及索引 3.select语句及insert语句 4.update语句与delete语句 5.SQL常用函数 6.多表连接及组合查询 7.视图操作及数据控制 参 ...

  7. Eclipse配置Tomcat,访问404错误

    我从官网上面下载的tomcat6,直接启动发现正常使用,但是在Eclipse绑定后启动,访问localhost:8080,本来应该是tomcat的主页,但是却报了404错误. 百度搜索了一下,原来是t ...

  8. uva-10562-二叉树

    题意: Homer教授被报道失踪了,我们怀疑这和他最近的研究有关,但是我们确实不知道他最近在研究什么. 侦探们试图侵入他的电脑,再几次失败后才意思到教授的智力超出他们很多............... ...

  9. oracle一个用户操作多个表空间中表的问题

    首先,授权给指定用户. 一个用户的默认表空间只能有一个,但是你可以试下用下面的语句为其授权在别的表空间中创建对像: alter user  username quota 0||unlimited on ...

  10. The type org.springframework.dao.support.DaoSupport cannot be resolved. It is indirectly referenced

    springmvc mybatis整合,遇到错误:The type org.springframework.dao.support.DaoSupport cannot be resolved. It ...