zoukankan      html  css  js  c++  java
  • [LintCode] 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.

    Example

    Given binary tree {3,9,20,#,#,15,7}

       3
      /
     /  
     9  20
        /
       /  
      15   7
    

    Return its vertical order traversal as:
    [[9],[3,15],[20],[7]]

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

     Analysis:  There are 3 rules that need to be followed.

    1. Each column's nodes are put in the same list.  Columns are processed from left to right.

    2. For each column, nodes of smaller row are added first.  Rows are processed from top to bottom.

    3. If both the column and row numbers are the same, the node that is on the left is added first.

    Rules 2 and 3 are met by a regular level order traversal.  To meet rule 1, we need to keep each node's 

    column number and have a fast way of adding the node to its corresponding column list. 

    To do this, we enqueue/dequeue a node's column number along the node itself during lever order traversal.

    A mapping between a column number and its corresponding node list is also created. 

     1 public List<List<Integer>> verticalOrder(TreeNode root) {
     2     List<List<Integer>> result = new ArrayList<List<Integer>>();
     3     if(root == null) {
     4         return result;
     5     }
     6     Queue<TreeNode> q1 = new LinkedList<TreeNode>();
     7     Queue<Integer> q2 = new LinkedList<Integer>();
     8     HashMap<Integer, List<Integer>> listMap = new HashMap<Integer, List<Integer>>();
     9     int minCol = 0; int maxCol = 0;
    10     q1.add(root);
    11     q2.add(0);
    12     
    13     while(!q1.isEmpty()) {
    14         TreeNode curr = q1.poll();
    15         int col = q2.poll();
    16         minCol = Math.min(minCol, col);
    17         maxCol = Math.max(maxCol, col);
    18         if(!listMap.containsKey(col)) {
    19             listMap.put(col, new ArrayList<Integer>());
    20         }
    21         listMap.get(col).add(curr.val);
    22         
    23         if(curr.left != null) {
    24             q1.add(curr.left);
    25             q2.add(col - 1);
    26         }
    27         if(curr.right != null) {
    28             q1.add(curr.right);
    29             q2.add(col + 1);
    30         }
    31     }
    32     
    33     for(int i = minCol; i <= maxCol; i++) {
    34         result.add(listMap.get(i));
    35     }
    36     return result;
    37 }  

    Related Problems 

    Binary Tree Level Order Traversal

  • 相关阅读:
    下载文件 文件名 乱码的问题.
    Powershell 变量
    20101227 [随笔] Roy
    20101022 [随笔] Roy
    20100927 [随笔] Roy
    20101209 [随笔] Roy
    20101123 [随笔] Roy
    20101213 [随笔] Roy
    第一 笔记
    分享Sokcet方式请求HTTP/HTTPS的封装类HttpHelper V2
  • 原文地址:https://www.cnblogs.com/lz87/p/7478925.html
Copyright © 2011-2022 走看看