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; }
* }
*/
class Solution {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
dfs(root, 0);
for (int i = min; i <= max; i++) {
res.add(new ArrayList<>());
} Queue<TreeNode> queue = new LinkedList<>();
Queue<Integer> indexQ = new LinkedList<>();
queue.offer(root);
// map the leftmost to 0, so that root is -min
indexQ.offer(-min);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
int curIndex = indexQ.poll();
res.get(curIndex).add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
indexQ.offer(curIndex - 1);
}
if (cur.right != null) {
queue.offer(cur.right);
indexQ.offer(curIndex + 1);
}
}
return res;
} private void dfs(TreeNode root, int num) {
if (root == null) {
return;
}
min = Math.min(min, num);
max = Math.max(max, num);
dfs(root.left, num - 1);
dfs(root.right, num + 1);
} }

[LC] 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

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

  3. 314. Binary Tree Vertical Order Traversal

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

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

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

  6. [Locked] Binary Tree Vertical Order Traversal

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

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

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

  8. LeetCode Binary Tree Vertical Order Traversal

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

  9. Binary Tree Vertical Order Traversal

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

随机推荐

  1. MongoDB三-高级操作

    复制来自:http://www.cnblogs.com/huangxincheng/archive/2012/02/21/2361205.html 今天跟大家分享一下mongodb中比较好玩的知识,主 ...

  2. kettle 数据库连接失败

    kettle 数据库连接失败 测试连接提示缺少驱动. 提示错误信息:Driver class 'oracle.jdbc.driver.OracleDriver' could not be found, ...

  3. xv6 系统调用

    1. 系统调用的实现 开发程序需所有的接口在user.h中,包含两部分system call和ulib user.h中的系统接口函数在usys.S中通过汇编实现 #define SYSCALL(nam ...

  4. Maven:maven依赖jar包冲突处理

    Maven多模块项目中偶尔会遇到这种问题:明明项目中相关的jar包已经通过pom.xml引进来了,还是报错找到不到相关的类的错误.这种时候,基本上可以断定是jar包冲突的问题. 很多情况下jar包会通 ...

  5. C语言历史

    如有错误,欢迎指出. 互帮互助,共同进步. 更新时间:2020-01-09 节选自<C语言程序设计现代方法>第2版 1.起源 C语言是贝尔实验室的Ken Thompson.Dennis R ...

  6. 干货 | AI人脸识别之人脸搜索

    本文档将利用京东云AI SDK来实践人脸识别中的人脸搜索功能,主要涉及到分组创建/删除.分组列表获取.人脸创建/删除.人脸搜索,本次实操的最终效果是:创建一个人脸库,拿一张图片在人脸库中搜索出相似度最 ...

  7. 吴裕雄--天生自然MySQL学习笔记:MySQL WHERE 子句

    MySQL 表中使用 SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是 SQL SELECT 语句使用 WHERE ...

  8. [Security] Web Security Essentials

    In this course, we'll learn how to exploit and then mitigate several common Web Security Vulnerabili ...

  9. Mybatix实现in查询(五)

    在这一节,我们要向大家介绍一下在Mybatis中想要实现in查询,Mapper文件应该怎么配置. 1)在com.mybatis.dao.PartDao中增加接口函数 public List<Pa ...

  10. Thread--线程间通信--管道

    在Java语言中提供了各种各样的输入/输出流Stream,使我们能够方便的对数据进行操作,其中管道流是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另一个线程从输入管道中读数 ...