题目:

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. 《梦断代码》读书笔记第0篇——“软件时间”、“死定了”、“Agenda之魂“

    第0章  软件时间 在未读这本书前,刚看到名字觉得是本讲代码的书,后来老师说是一个个的故事,这引起了我的兴趣,于是我便速速开始了第0章的阅读,读完一遍大概能读懂在讲什么,可能由于是译过来的书,书里面一 ...

  2. AlarmManager使用注意事项

    在使用AlarmManager实现闹钟需要注意的是,intent和pendingintend的context如果是activity,那么当activity回收之后,context对象则不能被Alarm ...

  3. MVC中使用SignalR打造酷炫实用的即时通讯功能附源码

    前言,现在这世道写篇帖子没个前言真不好意思发出来.本贴的主要内容来自于本人在之前项目中所开发的一个小功能,用于OA中的即时通讯.由于当时走的太急,忘记把代码拿出来.想想这已经是大半年前的事情了,时间过 ...

  4. VIMTUTOR《VIM教程》

    =============================================================================== =      欢     迎     阅 ...

  5. 设计模式之代理模式(Proxy)

    只能指针是代理模式的一种: 智能指针实现需要注意的问题: 1.构造函数指明显示构造. 2.拷贝构造函数,先断开前一个指针,然后用之前指针的值初始化现在的指针. 3.赋值函数需要先断开之前的指针,然后释 ...

  6. Hadoop分布式配置

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先参照Linux安 ...

  7. Javascript引擎单线程机制及setTimeout执行原理说明

    setTimeout用法在实际项目中还是会时常遇到.比如浏览器会聪明的等到一个函数堆栈结束后才改变DOM,如果再这个函数堆栈中把页面背景先从白色设为红色,再设回白色,那么浏览器会认为DOM没有发生任何 ...

  8. 深入理解jQuery的Event机制

    jQuery的Event模块非常强大.其功能远远比原生事件监听器强大许多,对同一个元素的监听只用一个eventListener,内部则是一个强大的观察者,根据匹配事件类型触发相应回调.jQuery不仅 ...

  9. 使用Assetbundle时可能遇到的坑

    原地址:http://www.cnblogs.com/realtimepixels/p/3652128.html 一 24 十一郎未分类 No Comments 转自 http://www.unity ...

  10. iOS-OpenGLES 简单渲染

     UIImage *showImage = [UIImage imageNamed:, , showImage.size.width, showImage.size.height);     ) fo ...