zoukankan      html  css  js  c++  java
  • 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:

    1. 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]
      ]
      
    2. Given binary tree [3,9,8,4,0,1,7],
           3
          /
         /  
         9   8
        /  /
       /  /  
       4  01   7
      

      return its vertical order traversal as:

      [
        [4],
        [9],
        [3,0,1],
        [8],
        [7]
      ]
      
    3. Given binary tree [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5),
           3
          /
         /  
         9   8
        /  /
       /  /  
       4  01   7
          /
         /  
         5   2
      

      return its vertical order traversal as:

      [
        [4],
        [9,5],
        [3,0,1],
        [8,2],
        [7]
      ]
      

    Analyses: Do BFS, have the root column as 0, record the leftmost column and rightmost column. Map the column as the node values in that column. 

     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> > result = new ArrayList<>();
    13         if (root == null) return result;
    14         
    15         // map the column to the nodes values in that column
    16         HashMap<Integer, List<Integer> > map = new HashMap<Integer, List<Integer> >();
    17         Queue<TreeNode> nodes = new LinkedList<TreeNode>();
    18         Queue<Integer> columns = new LinkedList<Integer>();
    19         
    20         nodes.add(root);
    21         columns.add(0);
    22         int left = 0, right = 0;
    23         while (nodes.peek() != null) {
    24             TreeNode node = nodes.remove();
    25             int column = columns.remove();
    26             
    27             if (!map.containsKey(column)) map.put(column, new ArrayList<Integer>());
    28             map.get(column).add(node.val);
    29             
    30             if (node.left != null) {
    31                 nodes.add(node.left);
    32                 columns.add(column - 1);
    33                 left = Math.min(left, column - 1);
    34             }
    35             
    36             if (node.right != null) {
    37                 nodes.add(node.right);
    38                 columns.add(column + 1);
    39                 right = Math.max(right, column + 1);
    40             }
    41         }
    42         
    43         for (int i = left; i <= right; i++) {
    44             List<Integer> temp = map.get(i);
    45             result.add(temp);
    46         }
    47         return result;
    48     }
    49 }
  • 相关阅读:
    (转)Nginx配置和内核优化 实现突破十万并发
    (转)资源监控工具Spotlight监测LINUX
    (转) linux I/O优化 磁盘读写参数设置
    (转)MongoDB numa系列问题三:overcommit_memory和zone_reclaim_mode
    (转)mongdb性能优化收集
    (转)部署MongoDB时需要注意的调参
    (转)Loadrunner监控Linux的17个指标
    (转)linux 内存管理——内核的shmall 和shmmax 参数
    (转)Linux性能调优之虚拟内存篇
    (转)LR监控Linux系统性能计数器详解
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6756381.html
Copyright © 2011-2022 走看看