Binary Tree Vertical Order Traversal
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的更多相关文章
- [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 ...
- 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, ...
- [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 ...
- 314. Binary Tree Vertical Order Traversal
题目: Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to ...
- [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 ...
- [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 ...
- Binary Tree Vertical Order Traversal -- LeetCode
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
随机推荐
- OC-方法的声明和实现、匿名对象
方法声明: 方法调用: *冒号也是方法名的一部分 *同一个类中不允许两个对象方法同名 练习 给Car类设计一个方法,用来和其他车比较车速,如果快返回1,慢返回-1,相同返回0 #import < ...
- Yii2 advanced版API接口开发 基于RESTful架构的 配置、实现、测试
参考地址: http://www.xiaoxiangzi.com/Programme/PHP/3348.html http://www.cnblogs.com/ganiks/p/yii2-restfu ...
- HTML5实战与剖析之触摸事件(touchstart、touchmove和touchend)(转)
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- linux下如何查看chm文件
转自:http://www.cnblogs.com/jesseZh/p/4036811.html (64位) sudo dpkg -i chmsee_1.3.0-2ubuntu2_amd64.d ...
- LINUX系统知识(转)
原文链接:http://blog.chinaunix.net/uid-725717-id-2060377.html 在Linux上配置好svnserve,通过eclipse访问,实现版本控制.但是开启 ...
- [设计模式] Javascript 之 观察者模式
观察者模式:定议 定义对象间的一种一对多的关系,当一个对象状态改变时 (一般称为被观察者),依赖于该对象的对象被通知,并更新; 观察者模式:说明 1. 观察者模式是行为模式,也被称为:发布-订阅模式. ...
- JSON的parse()和stringfy()方法
1.JSON.parse; 作用:将JavaScript对象表示法的JSON字符串转换为对象(字符串转对象). 语法:JSON.parse(text [, reviver]) text 必选. 一个有 ...
- C语言:输入输出
C语言无I/O语句,i/o操作由函数实现 #include<stdio.h> 字符输出函数putchar: 格式:putchar(c) 参数:c为字符常量,变量或者表达式 功能:把字符c输 ...
- VS上利用C#实现一个简单的串口程序记录
一.背景 工作上需要利用串口往下位机写入数据,VC太老,正好借此机会来熟悉一直很想接触的VS之C#. 感谢Tony托尼哥的串口通信代码,感谢梦真的C#的技术支持. 二.正文 1.项目架构:(以我现有的 ...
- Mac Pro 软件收藏
记录下Pro Mac中安装过的软件: 编号 软件名 功能说明 1 QQ 2 微信 3 搜狗输入法 4 Chrome 浏览器 Chrome 及其 插件“个性化设置”备份 5 360云盘 ...