题目:

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

链接: http://leetcode.com/problems/binary-tree-vertical-order-traversal/

题解:

二叉树Vertical order traversal。这道题意思很简单但例子举得不够好,假如上面第二个例子里5还有右子树的话,就会和20在一条column里。总的来说就是假定一个node的column是 i,那么它的左子树column就是i - 1,右子树column就是i + 1。我们可以用decorator模式建立一个TreeColumnNode,包含一个TreeNode,以及一个column value,然后用level order traversal进行计算就可以了,计算的时候用一个HashMap保存column value以及相同value的点。也要设置一个min column value和一个max column value,方便最后按照从小到大顺序获取hashmap里的值输出。这道题Discuss区Yavinci大神写得非常棒,放在reference里。

Time Complexity - O(n),  Space Complexity - O(n)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private class TreeColumnNode{
public TreeNode treeNode;
int col;
public TreeColumnNode(TreeNode node, int col) {
this.treeNode = node;
this.col = col;
}
} public List<List<Integer>> verticalOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) {
return res;
}
Queue<TreeColumnNode> queue = new LinkedList<>();
Map<Integer, List<Integer>> map = new HashMap<>();
queue.offer(new TreeColumnNode(root, 0));
int curLevel = 1;
int nextLevel = 0;
int min = 0;
int max = 0; while(!queue.isEmpty()) {
TreeColumnNode node = queue.poll();
if(map.containsKey(node.col)) {
map.get(node.col).add(node.treeNode.val);
} else {
map.put(node.col, new ArrayList<Integer>(Arrays.asList(node.treeNode.val)));
}
curLevel--; if(node.treeNode.left != null) {
queue.offer(new TreeColumnNode(node.treeNode.left, node.col - 1));
nextLevel++;
min = Math.min(node.col - 1, min);
}
if(node.treeNode.right != null) {
queue.offer(new TreeColumnNode(node.treeNode.right, node.col + 1));
nextLevel++;
max = Math.max(node.col + 1, max);
}
if(curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
}
} for(int i = min; i <= max; i++) {
res.add(map.get(i));
} return res;
}
}

Reference:

https://leetcode.com/discuss/75054/5ms-java-clean-solution

https://leetcode.com/discuss/73113/using-hashmap-bfs-java-solution

https://leetcode.com/discuss/74022/hashmap-bfs-solution-in-java

https://leetcode.com/discuss/73893/java-level-order-traversal-solution

http://algorithms.tutorialhorizon.com/print-the-binary-tree-in-vertical-order-path/

http://www.geeksforgeeks.org/print-binary-tree-vertical-order/

http://www.geeksforgeeks.org/print-binary-tree-vertical-order-set-2/

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. [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. [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. [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. [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. apache-activemq-5.14.0学习总结

    一.什么是ActiveMQ? 百度百科对ActiveMQ的描述:

  2. 团队小组开发nabc分析

    我们团队开发的项目为:牛逼的手电筒 NABC模型: 1.N:现在每个人几乎走哪,干什么都会拿着手机,而现在年轻人晚上在外面的也不少,所以手机里安装一个手电筒的APP还是很有必要的. 2.A:这学期正在 ...

  3. Vim实用命令

    [n]yy:从当前行复制n行 [n]p:粘贴n次 [n]dd:删除当前行往下的n行 /  : 向后查找 ?:向前查找 u → undo 撤销上一操作 <C-r> → redo 0 → 开启 ...

  4. Careercup - Google面试题 - 4699414551592960

    2014-05-06 13:34 题目链接 原题: we have a random list of people. each person knows his own height and the ...

  5. 使用Java 8 Lambda表达式对Employee类进行操作

    1,首先定义Employee类. package coffee.how.to.program.early.objects.chapter15; public class Employee { priv ...

  6. cygwin chmod 失效

    问题背景 为了在 Cygwin 下使用之前最喜爱的 screen 命令, 安装 Cygwin 时就选上了 screen 来运行一把 ganiks.liu@MAMIS-Gaiks-Liu /tmp $ ...

  7. 《我是IT一只小小鸟》读后感

    <我是IT一只小小鸟>读后感 首先,非常感谢我的老师给我推荐了这么一本书,虽然刚开始因为这门课学分太低,所以我对老师布置了字数这么多的作业存在有很大的不满,但在看了这本书后我的不满立马得到 ...

  8. SPOJ 057 Supernumbers in a permutation

    原题链接:http://www.spoj.com/problems/SUPPER/ 这道题n<=200000,那么确定为nlogn的算法,再定位到求LIS的O(nlogn)的算法. 对于每个a[ ...

  9. 【BZOJ】【1061】【NOI2008】志愿者招募

    网络流/费用流 OrzOrzOrz,这题太神了不会捉. 题解:https://www.byvoid.com/blog/noi-2008-employee/ 这道题正确的解法是构造网络,求网络最小费用最 ...

  10. Hibernate O/R Mapping模拟

    作为SSH中的重要一环,有必要理解一下Hibernate对 O/R Mapping的实现. 主要利用java的反射机制来得到完整的SQL语句. 准备工作: 1. Object Student实体类: ...