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 ...
随机推荐
- [Unity] 常用技巧收集
Unity 屏幕旋转 void Update () { //处理横向两个方向旋转 if(Input.deviceOrientation == DeviceOrientation.LandscapeLe ...
- YII2 Model 类切换数据库连接
配置多数据库: return [ // ... 'components' => [ // ... 'db' => [ 'class' => 'yii\db\Connection', ...
- list转map 键值对
Map<Long,Account> map = new HashMap<Long,Account>(); for(int i=0;i<list.size();i++){ ...
- CF460D Little Victor and Set (找规律)
D - Little Victor and Set Codeforces Round #262 (Div. 2) D D. Little Victor and Set time limit per t ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- java web
1,当访问完一个网页的时候,浏览器会有缓存,当你再次输入原来的网站是会有从远端传来的网页缓存,所以在测试的时候要清除缓存才行
- 新年新技术:HTTP/2
新的一年,项目也要带着发展的眼光往前走,得跟上潮流,当然前提是自己真的用的上. 用的上用不上都得先简单了解下. 2月下旬Google发布了首个基于HTTP/2的RPC框架GRPC,它是基于HTTP/2 ...
- PHP基础 之 数组(一)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- JS获取/设置iframe内对象元素、文档的几种方法
1.IE专用(通过frames索引形象定位): document.frames[i].document.getElementById('元素的ID'); 2.IE专用(通过iframe名称形象定位): ...
- MySQL源码分析以及目录结构 2
原文地址:MySQL源码分析以及目录结构作者:jacky民工 主要模块及数据流经过多年的发展,mysql的主要模块已经稳定,基本不会有大的修改.本文将对MySQL的整体架构及重要目录进行讲述. 源码结 ...