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. cf1200 E Compress Words(哈希)

    题意 有n个字符串,记为s1,s2……sn,s2与s1合并,合并的方式为:s1的后缀若与s2的前缀相同,就可以重叠起来,要最长的. 举个例子: “1333”  “33345” → “133345” s ...

  2. 高级css效果

    1.图片渐变效果 background linear-gradient(top,rgba(0,0,0,.8),rgba(0,0,0,.8))

  3. 81.常用的返回QuerySet对象的方法使用详解:values和values_list

    values: 指定提取的数据库表中的字段值,如果不指定任何的字段名的话,默认情况下会提取所有的字段值.但是需要注意的是使用values返回的QuerySet对象中包括的是一个个的字典. 1.提取与A ...

  4. 【从0到1学算法】大O表示法

    一般我们在选择算法时,都是想要选择效率最高的算法.那算法的效率,用什么表示?没错!就是用大O表示法. PS: 大O表示法中,log即为log2,后面不再说明. 下面以简单查找和二分查找,在含有n个元素 ...

  5. ArrayList集合的增、删、改、获取和长度

    API : code: package student; import java.util.ArrayList; public class ArrayListDemo { public static ...

  6. 简单的Vue计算属性

    倒转字符串 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  7. linux messages日志出现kernel: nf_conntrack: table full, dropping packet

    上述结果会让业务访问很慢!各种网络服务耗时大幅上升,各种time out,各种丢包,完全无法正常提供服务,大并发业务场景下,开防火墙很容易出现这种问题. 解决方法1:关闭分防火墙服务 解决方法2:修改 ...

  8. codeblocks无法找到编译器问题的三个原因

    出现如下提示: 查看了网上的资料, 1.编译器选择错误 查看路径:setting→compiler Selected compiler选取你已经安装且准备使用的编译器. 2.工程创建时配置错误 查看路 ...

  9. Java学习十四

    学习内容: 1.Junit 2.maven安装配置环境 一.Junit实例演示步骤 1.引入jar包 junit包需要引入hamcrest-core包,否则会报错 2.测试如下代码 package c ...

  10. h5与安卓、ios交互

    1.安卓交互 h5调用安卓方法 window.webview.xxx() 安卓调用h5方法, 方法需要在全局注册 window['showUnreadMsg'] = () => { this.$ ...