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

  • 相关阅读:
    做了48小时爸爸的感觉!
    利用Session和HashTable制作购物车
    vs2005控件演示之 Literal
    vs2005/.net2.0 控件实例之 下拉列表《DropDownList》
    vs2005/.net2.0 控件实例之 单选按纽和复选框 <RadioButton><CheckBox>
    vs2005/.net2.0 控件实例之 单选列表和复选列表
    一个被大多数初级程序员忽略的安全问题 [严重]
    使用Northwind和Entity框架的ASP.NET MVC实例发布
    对话Spring.NET
    揭开HTML 5工作草稿的神秘面纱
  • 原文地址:https://www.cnblogs.com/lz87/p/7478925.html
Copyright © 2011-2022 走看看