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 1:

Input: [3,9,20,null,null,15,7]

3
/\
/ \
9 20
/\
/ \
15 7 Output: [
[9],
[3,15],
[20],
[7]
]

思路

代码

 /**
* 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) {
// corner
List<List<Integer>> results = new ArrayList<>();
if (root == null) return results;
// init
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Map<Integer, List<Integer>> map = new HashMap<>();
Queue<Position> queue = new LinkedList<>(); //use queue to bfs
queue.add(new Position(root, 0));
while (!queue.isEmpty()) {
Position position = queue.remove();
min = Math.min(min, position.column);
max = Math.max(max, position.column);
List<Integer> list = map.get(position.column);
if (list == null) {
list = new ArrayList<>();
map.put(position.column, list);
}
list.add(position.node.val);
if (position.node.left != null) queue.add(new Position(position.node.left, position.column-1));
if (position.node.right != null) queue.add(new Position(position.node.right, position.column+1));
}
for(int i = min; i<= max; i++) {
List<Integer> list = map.get(i);
if (list != null) results.add(list);
}
return results;
}
} class Position {
TreeNode node;
int column;
Position(TreeNode node, int column) {
this.node = node;
this.column = column;
}
}

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

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

  4. LeetCode 314. Binary Tree Vertical Order Traversal

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

  5. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  6. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

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

  7. 314. Binary Tree Vertical Order Traversal

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

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

  9. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

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

随机推荐

  1. timus1745题解

    一.题目链接 http://acm.timus.ru/problem.aspx?space=1&num=1745 二.题意 给定$n$个由'('和')'组成的字符串,每个串最多只能使用$1$次 ...

  2. PyCharm License Activation激活码失效问题的解决方法

    自己的小Python项目好几天没有写了,今天打开PyCharm准备继续写,突然发现之前的激活码被取消不能用了,本来激情满满的准备干活啦!之前搜的这个激活码本来说的是可以用到18年1月份的呢,哎…心情是 ...

  3. [Python] numpy.random.rand

    numpy.random.rand numpy.random.rand(d0, d1, ..., dn) Random values in a given shape. Create an array ...

  4. Jenkins配置HTML报告(Windows环境)

    1.首先安装插件HTML Publisher,点击直接安装 2.在任务中配置,构建后操作,添加Publish HTML reports 3.添加完成后,新增一项 4.HTML directory to ...

  5. vb 水晶报表打印

    vb里面的水晶报表打印控件:CrystalReportViewer 用到的dll文件: 水晶报表打印其实很简单,只要创建报表对象,再对其传递数据就可以打印出来.当然所传递的数据要与水晶报表设计里面的数 ...

  6. LRU的理解与Java实现

    简介 LRU(Least Recently Used)直译为"最近最少使用".其实很多老外发明的词直译过来对于我们来说并不是特别好理解,甚至有些词并不在国人的思维模式之内,比如快速 ...

  7. bootstrap修改勾选样式

    小对勾需要引入awesome插件. css部分: .bella-checkbox{ position: relative;}/** 将初始的checkbox的样式改变 */.bella-checkbo ...

  8. localhost 127.0.0.1

    No1: localhost也叫local ,正确的解释是:本地服务器 127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器) 他们的解析通过本机的host文件,windows自 ...

  9. mysql数据库复制

    核心命令是 myssqldump mysqldump --host=host1 --opt sourceDb| mysql --host=host2 -C targetDb 详情参考: MySQL数据 ...

  10. jsfl 巧用获取jsfl绝对路径,导入配置文件,注意配置文件无法改变舞台宽高

    //获取jsfl下的AS3.xml配置文件的路径 var jsflURL_arr=fl.scriptURI.split("/"); jsflURL_arr.splice(jsflU ...