原题链接在这里: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 Binary Tree Vertical Order Traversal的更多相关文章

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

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

  3. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  4. [Locked] Binary Tree Vertical Order Traversal

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

  5. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

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

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

  8. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  9. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

随机推荐

  1. Python 打包和发布方法汇总

    以下主要Python打包汇总,作为学习备份: 一.使用python内部基础工具包Distutils打包程序 1. 在打包之前需要做的就是配置好安装脚本,一般为setup.py文件: 示例(setup. ...

  2. 常见开发需求之js处理url汉字编码中的乱码

    需求及解决    两个页面传值的需求是很常见的,angular中有很多常见的方法用于传值,而且都不会受到字符编码的影响,而采用传统的url中拼字符串进行传值的操作,如果拼串中涉及到中文字符,我们就要考 ...

  3. 【Mybatis架构】输入、输出映射

    前言综述:   其实在我们分析Mybatis的查询缓存或者是一些简介的时候,我们就不难看到有关于Mybatis输入输出映射的东西,比如说: 但是一直没有想起来系统的来总结一下这方面的相关知识,偶然看到 ...

  4. Go语言 数组

    介绍 Array 是值类型,Slice 和 Map 是引用类型.他们是有很大区别的,尤其是在参数传递的时候. 另外,Slice 和 Map 的变量 仅仅声明是不行的,必须还要分配空间(也就是初始化,i ...

  5. STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱?

    STM32解密STM32F103芯片解密STM32F103R6单片机破解多少钱? STM32F系列单片机芯片解密型号: STM32F100  |  STM32F101  |  STM32F102  | ...

  6. 解决rand()伪随机数

    利用time改变种子 例: #include <stdlib.h> #include <stdio.h> #include <time.h>//使用当前时钟做种子 ...

  7. python 安装模块

    python安装模块的方法很多,在此仅介绍一种,不需要安装其他附带的pip等,python安装完之后,配置环境变量,我由于中英文分号原因,环境变量始终没能配置成功汗. 1:下载模块的压缩文件解压到任意 ...

  8. 【转】java-String中的 intern()

    转自:http://blog.sina.com.cn/s/blog_69dcd5ed0101171h.html 1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值 ...

  9. Codeforces Round #FF(255) DIV2

    A - DZY Loves Hash 水题,开辟一个数组即可 #include <iostream> #include <vector> #include <algori ...

  10. div垂直居中的几种方法

    CSS教程:div垂直居中的N种方法[转](原文地址:http://www.cnblogs.com/chuncn/archive/2008/10/09/1307321.html) 在说到这个问题的时候 ...