原题链接在这里: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;
}
}
}

类似Vertical Order Traversal of a Binary Tree.

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

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

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

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

  4. 314. Binary Tree Vertical Order Traversal

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

  5. [LC] 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. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. [Locked] Binary Tree Vertical Order Traversal

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

  8. Java for LeetCode 107 Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

随机推荐

  1. win7 重启dns

    安装xshell.百度一搜就下载了. 修改hosts,hosts路径 C:\Windows\System32\drivers\etc\hosts 写法和linux一样. 重启dns命令 ipconfi ...

  2. Linux文件系统管理 常见命令df、du、fsck、dumpe2fs

    概述 Linux文件系统管理 常见命令df.du.fsck.dumpe2fs. 文件系统查看命令:df 通过 df 命令可以査看已经挂载的文件系统的信息包括设备文件名.文件系统总大小.已经使用的大小. ...

  3. @MarkFan 口语练习录音 20140406 [美女与野兽的口语练习录音]

    大家好,您现在收听的是美女与野兽的口语练习录音 敢于追求,不惧任何挑战,才是勇敢的人生.试想一下,世界上每天有多少人为了梦想,为了生活,甚至是为了别人在不停地奔跑.假若你此刻心中装有梦想,却碍于现实不 ...

  4. codeforces上某题

    一道codeforces上的题目. 题目大意: 定义有k个不同的字符的字符串为好字符串.现在给出一个字符串,求解对该字符串的每个前缀Si至少是多少个好字符串的连接,若不能由好字符串连接而成则输出-1. ...

  5. 吴恩达深度学习笔记(五) —— 优化算法:Mini-Batch GD、Momentum、RMSprop、Adam、学习率衰减

    主要内容: 一.Mini-Batch Gradient descent 二.Momentum 四.RMSprop 五.Adam 六.优化算法性能比较 七.学习率衰减 一.Mini-Batch Grad ...

  6. tomcat常见面试题1

    一.Tomcat的缺省是多少,怎么修改 Tomcat的缺省端口号是8080. 修改Tomcat端口号: 1.找到Tomcat目录下的conf文件夹 2.进入conf文件夹里面找到server.xml文 ...

  7. mysql里的ibdata1文件

    mysql大多数磁盘空间被 InnoDB 的共享表空间 ibdata1 使用.而你已经启用了 innodb_file_per_table,所以问题是: ibdata1存了什么? 当你启用了innodb ...

  8. 把已安装的wampserver移动到不同目录使用应注意的问题

    很多时候需要把已安装的wampserver移动到不同目录使用,此时应注意几个问题: 1.修改D:\wamp64\bin\apache\apache2.4.9\conf目录下的httpd.conf文件( ...

  9. 用java.lang.Math.random()语句,随机输出{size:自定义参数}个数不重复并且按顺序从小到大排列(冒泡排序)

    package com.test; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lan ...

  10. 分布式技术 webservice

    web service 是一个平台独立的.低耦合的.自包含的.基于编程的web的应用程序,可使用开发的XML(标准通用标记语言下的一个字表)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布 ...