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:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its vertical order traversal as:

[
[9],
[3,15],
[20],
[7]
]

Given binary tree [3,9,20,4,5,2,7],

    _3_
/ \
9 20
/ \ / \
4 5 2 7

return its vertical order traversal as:

[
[4],
[9],
[3,5,2],
[20],
[7]
]
分析:
For root, col = 0. The col of root's left child is root.col - 1, and the col of root's right child is root.col + 1.
public class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
} Map<Integer, List<Integer>> col_nodes = new HashMap<>();
Map<TreeNode, Integer> node_col = new HashMap<>();
node_col.put(root, ); Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int min = ;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
int col = node_col.get(node);
if (!col_nodes.containsKey(col)) {
col_nodes.put(col, new ArrayList<>());
}
col_nodes.get(col).add(node.val); if (node.left != null) {
queue.add(node.left);
node_col.put(node.left, col - );
}
if (node.right != null) {
queue.add(node.right);
node_col.put(node.right, col + );
}
min = Math.min(min, col);
}
while (col_nodes.containsKey(min)) {
res.add(col_nodes.get(min++));
}
return res;
}
}
 public class Solution {
public List<List<Integer>> verticalOrder(TreeNode root) {
Map<Integer, List<Integer>> col_nodes = new HashMap<>();
helper(root, , col_nodes);
return getColNodes(col_nodes);
} private List<List<Integer>> getColNodes(Map<Integer, List<Integer>> col_nodes) {
List<List<Integer>> res = new ArrayList<>();
Integer min = col_nodes.keySet().stream().min((i, j) -> i.compareTo(j)).get();
while (col_nodes.containsKey(min)) {
res.add(col_nodes.get(min));
min++;
}
return res;
} private void helper(TreeNode root, int col, Map<Integer, List<Integer>> col_nodes) {
if (root == null) return;
List<Integer> nodes = col_nodes.getOrDefault(col, new ArrayList<>());
nodes.add(root.value);
col_nodes.put(col, nodes);
helper(root.left, col - , col_nodes);
helper(root.right, col + , col_nodes);
}
}

Reference:

https://discuss.leetcode.com/topic/31115/using-hashmap-bfs-java-solution

http://www.cnblogs.com/yrbbest/p/5065457.html

http://www.programcreek.com/2014/04/leetcode-binary-tree-vertical-order-traversal-java/

Binary Tree Vertical Order Traversal的更多相关文章

  1. [Locked] Binary Tree Vertical Order Traversal

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

  2. [LeetCode] 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 Binary Tree Vertical Order Traversal

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

  4. LeetCode 314. Binary Tree Vertical Order Traversal

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

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

  6. 314. Binary Tree Vertical Order Traversal

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

  7. [Swift]LeetCode314. 二叉树的竖直遍历 $ Binary Tree Vertical Order Traversal

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

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

  9. Binary Tree Vertical Order Traversal -- LeetCode

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

随机推荐

  1. Java序列化的几种方式以及序列化的作用

    Java序列化的几种方式以及序列化的作用 本文着重讲解一下Java序列化的相关内容. 如果对Java序列化感兴趣的同学可以研究一下. 一.Java序列化的作用    有的时候我们想要把一个Java对象 ...

  2. 使用Lucene开发自己的搜索引擎

    1.下载Lucene开发包,请到:http://lucene.apache.org/ 2.在myeclipse环境部署该开发包: 3.代码编写: package Lucene; import java ...

  3. ASP.NET MVC使用Bootstrap系列(1)——开始使用Bootstrap

    阅读目录 Bootstrap结构介绍 在ASP.NET MVC 项目中添加Bootstrap文件 为网站创建Layout布局页 使用捆绑打包和压缩来提升网站性能 在Bootstrap项目中使用捆绑打包 ...

  4. SqlServer 18456 错误解决方案

    sqlserver 安装需注意事项:  不要选择,否则运行一段时间后sqlserver登录时会出现18456错误. 发生18456错误(sa 可登录的情况下)解决本地用户登录问题的方案: <1& ...

  5. MySQL里的found_row()与row_count()的解释及用法

    MySQL中有两个函数来计算上一条语句影响了多少行,不同于SqlServer/Oracle,不要因为此方面的差异而引起功能问题   出处:mysqlpub.com MySQL中有两个函数来计算上一条语 ...

  6. webapi支持跨域访问

    写在前面 在实际应用中,跨域请求还是比较常见的,如何上接口直接支持跨域的访问呢? demo 场景项目A有个接口用来获取用户列表,现在项目b也有个功能需要加载用户列表.这两个项目在两个域名下,至少端口好 ...

  7. oracle 中的dual表简介与用法

    Dual表是每个数据库创建时默认生成的,该表仅有一列一行. 1)分析dual表执行,如下:

  8. ie8及其以下浏览器的document.getElementsByClassName兼容性问题

    使用JavaScript访问DOM的一个重大问题是,此过程需要一种通过元素类名称来选择类的类函数,对DOMContentReady,这种类函数的缺失导致开发人员需要自己编写自定义脚本业执行上述任务,许 ...

  9. 让ie浏览器支持html5新标签的解决方法(使用html5shiv)

    没估计错的话旧版浏览器都是不识别这些新增的标签所以都是用行内元素来处理解决的,所以,有一个解决办法的突破口就是让它变成块状元素就不会处于同一行了,这样在新旧浏览器都是可以显示同样的效果,再者就是让浏览 ...

  10. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)

    urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的一些 ...