原题链接在这里: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. loadrunder之脚本篇——Run-time Settings之Pacing

      As soon as the previous iteration ends 前一个迭代一结束就尽可能快的开始新一轮的迭代   After the previous iteration ends ...

  2. 字典树 trie树 学习

    一字典树 字典树,又称单词查找树,Trie树,是一种树形结构,哈希表的一个变种   二.性质 根节点不包含字符,除根节点以外的每一个节点都只包含一个字符: 从根节点到某一节点,路径上经过的字符串连接起 ...

  3. Android开发之旅-Fragment和Activity之间onCreateOptionsMenu的联系

    Fragment和Activity一样,可以重写onCreateOptionsMenu方法来设定自己的菜单,其实这两个地方使用onCreateOptionsMenu的目的和效果都是完全一样的,但是由于 ...

  4. 系统封装接口层 cmsis_os

    在这个实时操作系统泛滥的年代,有这么一个系统封装接口层还是蛮有必要的.前些时间偶然间在STM32最新的固件库中就发现了这个系统封装接口,当时就把自己所用的系统进行封装.直到最近KEIL5.0发现其中所 ...

  5. linux上安装程序出现的问题汇总

    1.程序在编译过程中出现:variable set but not used [-Werror=unused-but-set-variable] 解决方法:将configure文件和Makefile文 ...

  6. iOS下的WiFi开发

    iOS下Wi-Fi开发需要添加依赖库SystemConfiguration.framework,在需要使用Wi-Fi信息的控制器下引入头文件#import <SystemConfiguratio ...

  7. latin-1

    Latin1是ISO-8859-1的别名,有些环境下写作Latin-1.ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII ...

  8. Introduction to vSphere Integrated Containers

    vSphere Integrated Containers enables IT teams to seamlessly run traditional workloads and container ...

  9. java resources 红叉 Cannot change version of project facet Dynamic Web Module to 2.5

    在使用maven导入项目的时候,markers提示Cannot change version of project facet Dynamic Web Module to 2.5,不能将工程转换为2. ...

  10. Linux Ctrl+Z VS Ctrl+C 以及+Z的使用方法

    问题及处理: Ctrl+Z是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用fg/bg操作继续前台或后台的任务,fg命令重新启动前台被中断的任务,bg命令把被中断的任 ...