LeetCode Binary Tree Vertical Order Traversal
原题链接在这里:https://leetcode.com/problems/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]
]
题解:
与Binary Tree Level Order Traversal类似。
BFS, 把TreeNode和它所在的col分别放到两个queue中. dequeue后放TreeNode到对应的 colunm bucket里.
Example of [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
. Notice that every child access changes one column bucket id. So 12
actually goes ahead of 11
.
Time Complexity: O(n). Space: O(n).
AC Java:
/**
* 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) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
HashMap<Integer, List<Integer>> colBucket = new HashMap<Integer, List<Integer>>();
LinkedList<TreeNode> que = new LinkedList<TreeNode>();
LinkedList<Integer> cols = new LinkedList<Integer>();
int min = 0;
int max = 0; que.add(root);
cols.add(0);
while(!que.isEmpty()){
TreeNode tn = que.poll();
int col = cols.poll();
if(!colBucket.containsKey(col)){
colBucket.put(col, new ArrayList<Integer>());
}
colBucket.get(col).add(tn.val);
min = Math.min(min, col);
max = Math.max(max, col); if(tn.left != null){
que.add(tn.left);
cols.add(col-1);
}
if(tn.right != null){
que.add(tn.right);
cols.add(col+1);
}
}
for(int i = min; i<=max; i++){
res.add(colBucket.get(i));
}
return res;
}
}
便于理解,可以吧TreeNode和它对应的column number放在一个Pair里.
Time Complexity: O(n). Space: O(n).
AC Java:
/**
* 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) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
} HashMap<Integer, List<Integer>> hm = new HashMap<Integer, List<Integer>>();
LinkedList<Pair> que = new LinkedList<Pair>();
que.add(new Pair(root, 0));
int min = 0;
int max = 0;
while(!que.isEmpty()){
Pair cur = que.poll();
if(!hm.containsKey(cur.col)){
hm.put(cur.col, new ArrayList<Integer>());
}
hm.get(cur.col).add(cur.tn.val);
max = Math.max(max, cur.col);
min = Math.min(min, cur.col); if(cur.tn.left != null){
que.add(new Pair(cur.tn.left, cur.col-1));
}
if(cur.tn.right != null){
que.add(new Pair(cur.tn.right, cur.col+1));
}
}
for(int i = min; i<=max; i++){
res.add(hm.get(i));
}
return res;
} public class Pair{
TreeNode tn;
int col;
public Pair(TreeNode tn, int col){
this.tn = tn;
this.col = col;
}
}
}
LeetCode Binary Tree Vertical Order Traversal的更多相关文章
- [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 Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- [Locked] Binary Tree Vertical Order Traversal
Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...
- 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 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [leetcode]Binary Tree Level Order Traversal II @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
随机推荐
- Win10 UI入门RelativePanel
<RelativePanel Background="Black" > <Rectangle x:Name=" RelativePanel.AlignH ...
- WPF程序在Windows 7下应用Windows 8主题
这篇博客介绍如何在Windows 7下应用Windows 8的主题. 首先我们先看一个很常见的场景,同样的WPF程序(样式未重写)在不同的操作系统上展示会有些不同.这是为什么呢?WPF程序启动时会加载 ...
- linux协议栈skb操作函数
- [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放
貌似有一段时间没更新2dtoolkit系列了,这段时间一直在忙着其他事情,今天开始继续这个插件系列的教程,网上搜索,貌似关于这个插件的教程无非还是跟官方的教程很类似,有的甚至都没有自己照着亲手实践一遍 ...
- 注解:【有连接表的】Hibernate双向1->N关联(仅N端控制关联关系)
Person与Address关联:双向1->N,[有连接表的],N端控制关联关系 Person.java package org.crazyit.app.domain; import java. ...
- hdu 4547(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 思路:这题的本质还是LCA问题,但是需要注意的地方有: 1.如果Q中u,v的lca为u,那么只需 ...
- CodeForces Round#313
第一题想当然了,结果被坑.. 有1的肯定能构成所有的其他数,没有1的肯定构不成1 ,这题T T #include <iostream> #include <cstring> # ...
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- spark API 介绍链接
spark API介绍: http://homepage.cs.latrobe.edu.au/zhe/ZhenHeSparkRDDAPIExamples.html#aggregateByKey
- 从零开始山寨Caffe·叁:全局线程管理器
你需要一个管家,随手召唤的那种,想吃啥就吃啥. ——设计一个全局线程管理器 一个机器学习系统,需要管理一些公共的配置信息,如何存储这些配置信息,是一个难题. 设计模式 MVC框架 在传统的MVC编程框 ...