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. PostgreSQL下安装pg_stat_statements

    一.安装过程如下:进入postgreSQL安装包的contrib/pg_stat_statements目录,执行编译和安装动作:用root用户 make && make install ...

  2. windows hbase installation

    In the previous post,  I have introduced how to install hadoop on windows based system. Now, I will ...

  3. 更新OpenSSH

    1.安装必要组件: yum install -y gcc openssl-devel pam-devel rpm-build 2.下载OpenSSH最新版本: https://ftp.openbsd. ...

  4. 位运算骚操作 Part 2

    ▶ 计算 unsigned int v 的以 2 为底的对数,结果放入 unsigned int r . // 方法零 #pragma unroll ;v; r++, v >>= ); / ...

  5. iframe中子页面父页面里函数互调

    在iframe中很多要用的子页面父页面函数互调的情况,下面看一下各自用法,本人写个人网站的时候用过其他场景尚未试过 子页面调父页面 function fu(){ alert('父'); } funct ...

  6. c# DbProviderFactories 多数据库支持工程模式

    DbProviderFactories.GetFactory(dbProviderName) DBProviderFactory factory = DBProviderFactorys.GetFac ...

  7. 揭秘Java架构技术体系

    Web应用,最常见的研发语言是Java和PHP. 后端服务,最常见的研发语言是Java和C/C++. 大数据,最常见的研发语言是Java和Python. 可以说,Java是现阶段中国互联网公司中,覆盖 ...

  8. 队列queue实例(生产者和消费者模型)

    import queue, threading, time q = queue.Queue(maxsize=10)def producter(n): count = 1 while True: q.p ...

  9. gradle问题 cordova

    cordova升级7.0后,运行 > ionic build android  或者 cordova build android     报出错误 Error: Could not find a ...

  10. Supervisor 进程管理工具

    简介: Supervisor 进程管理工具 一.安装 shell > yum -y install python-pip shell > pip install supervisor # ...