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

题目:

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

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its vertical order traversal as:

  1. [
  2. [9],
  3. [3,15],
  4. [20],
  5. [7]
  6. ]

Given binary tree [3,9,20,4,5,2,7],

  1. _3_
  2. / \
  3. 9 20
  4. / \ / \
  5. 4 5 2 7

return its vertical order traversal as:

  1. [
  2. [4],
  3. [9],
  4. [3,5,2],
  5. [20],
  6. [7]
  7. ]

题解:

Binary Tree Level Order Traversal类似。

BFS, 把TreeNode和它所在的col分别放到两个queue中. dequeue后放TreeNode到对应的 colunm bucket里.

Example of [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]. Notice that every child access changes one column bucket id. So 12 actually goes ahead of 11.

Time Complexity: O(n). Space: O(n).

AC Java:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<List<Integer>> verticalOrder(TreeNode root) {
  12. List<List<Integer>> res = new ArrayList<List<Integer>>();
  13. if(root == null){
  14. return res;
  15. }
  16. HashMap<Integer, List<Integer>> colBucket = new HashMap<Integer, List<Integer>>();
  17. LinkedList<TreeNode> que = new LinkedList<TreeNode>();
  18. LinkedList<Integer> cols = new LinkedList<Integer>();
  19. int min = 0;
  20. int max = 0;
  21.  
  22. que.add(root);
  23. cols.add(0);
  24. while(!que.isEmpty()){
  25. TreeNode tn = que.poll();
  26. int col = cols.poll();
  27. if(!colBucket.containsKey(col)){
  28. colBucket.put(col, new ArrayList<Integer>());
  29. }
  30. colBucket.get(col).add(tn.val);
  31. min = Math.min(min, col);
  32. max = Math.max(max, col);
  33.  
  34. if(tn.left != null){
  35. que.add(tn.left);
  36. cols.add(col-1);
  37. }
  38. if(tn.right != null){
  39. que.add(tn.right);
  40. cols.add(col+1);
  41. }
  42. }
  43. for(int i = min; i<=max; i++){
  44. res.add(colBucket.get(i));
  45. }
  46. return res;
  47. }
  48. }

便于理解,可以吧TreeNode和它对应的column number放在一个Pair里.

Time Complexity: O(n). Space: O(n).

AC Java:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<List<Integer>> verticalOrder(TreeNode root) {
  12. List<List<Integer>> res = new ArrayList<List<Integer>>();
  13. if(root == null){
  14. return res;
  15. }
  16.  
  17. HashMap<Integer, List<Integer>> hm = new HashMap<Integer, List<Integer>>();
  18. LinkedList<Pair> que = new LinkedList<Pair>();
  19. que.add(new Pair(root, 0));
  20. int min = 0;
  21. int max = 0;
  22. while(!que.isEmpty()){
  23. Pair cur = que.poll();
  24. if(!hm.containsKey(cur.col)){
  25. hm.put(cur.col, new ArrayList<Integer>());
  26. }
  27. hm.get(cur.col).add(cur.tn.val);
  28. max = Math.max(max, cur.col);
  29. min = Math.min(min, cur.col);
  30.  
  31. if(cur.tn.left != null){
  32. que.add(new Pair(cur.tn.left, cur.col-1));
  33. }
  34. if(cur.tn.right != null){
  35. que.add(new Pair(cur.tn.right, cur.col+1));
  36. }
  37. }
  38. for(int i = min; i<=max; i++){
  39. res.add(hm.get(i));
  40. }
  41. return res;
  42. }
  43.  
  44. public class Pair{
  45. TreeNode tn;
  46. int col;
  47. public Pair(TreeNode tn, int col){
  48. this.tn = tn;
  49. this.col = col;
  50. }
  51. }
  52. }

类似Vertical Order Traversal of a Binary Tree.

LeetCode Binary Tree Vertical Order Traversal的更多相关文章

  1. [LeetCode] 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] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  4. [Locked] Binary Tree Vertical Order Traversal

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

  5. LeetCode 314. Binary Tree Vertical Order Traversal

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

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

  7. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  9. LeetCode: Binary Tree Level Order Traversal 解题报告

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

随机推荐

  1. Schwarz积分公式

    设$f\in H(B(0,R))\cap C(\overline{B(0,R)})$,且$f=u+iv$,则$f$可用其实部表示为 $$f(z)=\frac{1}{2\pi}\int_{0}^{2\p ...

  2. VR技术的系统化实现阶段

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 从20世纪80年代至80年代中期,虚拟现实技术的基本概念开始逐渐形成和完善.这一时期出现了一些比较经典的 ...

  3. NSString使用stringWithFormat拼接的相关知识

    NSString使用stringWithFormat拼接的相关知识 保留2位小数点 1 2 3 4 //.2代表小数点后面保留2位(2代表保留的数量) NSString *string = [NSSt ...

  4. mysql远程连接问题

    问题:在服务器里面新安装一个MYSQL数据库,结果在远程电脑连接不上,并提示“服务器连接错误Host 'XXX' is not allowed to connect to this MySQL ser ...

  5. SqlServer性能检测和优化工具使用详细(转)

    转载链接:http://www.cnblogs.com/knowledgesea/p/3683505.html 工具概要 如果你的数据库应用系统中,存在有大量表,视图,索引,触发器,函数,存储过程,s ...

  6. js实现判断大写锁定是否开启(转)

    转载地址:http://www.cnblogs.com/xiaoao808/archive/2008/07/31/1257624.html 在用户登录输入密码时,常常会有因为大写锁定开启而造成输入密码 ...

  7. 【Java EE 学习 34】【struts2学习第一天】

    一.struts2简介 struts2是一个用来开发MVC应用程序的框架.它提供了Web应用程序开发过程中的一些常见问题的解决方案. 1.struts2的作用域范围:三层架构当中的第一层,相当于MVC ...

  8. curl命令行使用

    curl 命令使用   原文地址:http://blog.sina.com.cn/s/blog_4b9eab320100slyw.html 可以看作命令行浏览器 1.开启gzip请求curl -I h ...

  9. BACKLOG

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  10. LightOJ1171 Knights in Chessboard (II)(二分图最大点独立集)

    题目 Source http://www.lightoj.com/volume_showproblem.php?problem=1171 Description Given an m x n ches ...