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 ...
随机推荐
- VS2015 推荐插件
VS2015 推荐插件 //////////////////////////////////////////////////////////////////////////////////////// ...
- git之https或http方式设置记住用户名和密码的方法
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credenti ...
- Linq_Lambda GroupBy使用笔记
今天看MVC遇到了GroupBY的Lambda表达式..有兴趣详细的看下去..得此笔记..记录之... 不罗嗦..上代码... //得到List<GroupEmail>对象 数据源 var ...
- WebService 之 WSDL文件 讲解
原文地址:http://blog.csdn.net/tropica/archive/2008/11/02/3203892.aspx 恩,我想说的是,是不是经常有人在开发的时候,特别是和第三方有接口的时 ...
- 大熊君大话NodeJS之------Net模块
一,开篇分析 从今天开始,我们来深入具体的模块学习,这篇文章是这个系列(大熊君大话NodeJS)文章的第三篇,前两篇主要是以理论为主,相信大家在前两篇的学习中, 对NodeJS也有一个基本的认识,没事 ...
- flask 知识点总结
============================request对象的常用属性============================具体使用方法如下:request.headers, requ ...
- 使一个div始终显示在页面中间
使一个div始终显示在页面中间 假设我们有一个div层:<div id=”myDiv”></div> 首先,我们用css来控制它在水平上始终居中,那么我们的css代码应该是这样 ...
- JVM初探 -JVM内存模型
JVM初探 -JVM内存模型 标签 : JVM JVM是每个Java开发每天都会接触到的东西, 其相关知识也应该是每个人都要深入了解的. 但接触了很多人发现: 或了解片面或知识体系陈旧. 因此最近抽时 ...
- MFC线程内操作主窗体 控件
CWnd* h_d2 = AfxGetApp()->GetMainWnd(); //获取主窗口的句柄 h_d2-> GetDlgItem(IDC_EDIT2)->GetWindowT ...
- C#获取文件/字节数组MD5值方法
找了很多,就这个管用,有时间好好研究一番 public static string GetMD5Hash(string fileName) { try { FileStream file = new ...