zoukankan      html  css  js  c++  java
  • Print a Binary Tree in Vertical Order

    Given a binary tree, print it vertically. The following example illustrates vertical order traversal.

               1
            /    
           2      3
          /     / 
         4   5  6   7
                    
                  8   9 
                   
    			  
    The output of print this tree vertically will be:
    4
    2
    1 5 6
    3 8
    7
    9 

    We strongly recommend to minimize the browser and try this yourself first.

    The idea is to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is -2 (for node with value 4) and maximum distance is 3 (For node with value 9).
    Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum from root, and for each vertical line traverse the tree and print the nodes which lie on that vertical line.

    Algorithm:

    // min --> Minimum horizontal distance from root
    // max --> Maximum horizontal distance from root
    // hd  --> Horizontal distance of current node from root 
    findMinMax(tree, min, max, hd)
         if tree is NULL then return;
     
         if hd is less than min then
               min = hd;
         else if hd is greater than max then
              *max = hd;
     
         findMinMax(tree->left, min, max, hd-1);
         findMinMax(tree->right, min, max, hd+1);
    
     
    printVerticalLine(tree, line_no, hd)
         if tree is NULL then return;
     
         if hd is equal to line_no, then
               print(tree->data);
         printVerticalLine(tree->left, line_no, hd-1);
         printVerticalLine(tree->right, line_no, hd+1); 

     1 //Print a Binary Tree in Vertical Order 
     2 static int min;
     3 static int max;
     4 static HashMap<Integer,ArrayList> map;
     5 
     6 public static void generate(TreeNode root, Integer dis){
     7     if(root == null) return;
     8     else{
     9         if(map.containsKey(dis)) map.get(dis).add(root.val);
    10         else{
    11             ArrayList<Integer> tmp = new ArrayList<Integer> ();
    12             tmp.add(root.val);
    13             map.put(dis, tmp);
    14         }
    15     }
    16     generate(root.left, dis - 1);
    17     generate(root.right, dis + 1);
    18 }
    19 
    20 public static ArrayList<Integer>[] verticalOrderTraveralBT(TreeNode root){
    21     min = 0; max = 0;
    22     int len = max - min + 1;
    23     map = new HashMap<Integer,ArrayList>();
    24     generate(root, 0);
    25     return map.values();
    26 }
    27 
    28 
    29 public static void main(String[] args) {    
    30     TreeNode root = new TreeNode(1);
    31     root.left = new TreeNode(2);
    32     root.right = new TreeNode(3);
    33     root.left.left = new TreeNode(4);
    34     root.left.right = new TreeNode(5);
    35     root.right.left = new TreeNode(6);
    36     root.right.right = new TreeNode(7);
    37     root.right.left.right = new TreeNode(8);
    38     root.right.right.right = new TreeNode(9);
    39     ArrayList[] result = verticalOrderTraveralBT(root);
    40     for(int i = 0; i < max - min + 1; i ++){
    41         System.out.println(result[i].toString());
    42     }
    43 }

    也可以使用HashMap 来做, 这样就不需要min / max 了。

     1 //Print a Binary Tree in Vertical Order 
     2 static int min;
     3 static int max;
     4 static ArrayList<Integer>[] output;
     5 public static void findMinMax(TreeNode root, Integer dis){
     6     if(root == null) return;
     7     else{
     8         min = Math.min(dis, min);
     9         max = Math.max(dis, max);
    10     }
    11     findMinMax(root.left, dis - 1);
    12     findMinMax(root.right, dis + 1);
    13 }
    14 
    15 public static void generate(TreeNode root, Integer dis){
    16     if(root == null) return;
    17     else{
    18         output[dis - min].add(root.val);
    19     }
    20     generate(root.left, dis - 1);
    21     generate(root.right, dis + 1);
    22 }
    23 
    24 public static ArrayList<Integer>[] verticalOrderTraveralBT(TreeNode root){
    25     min = 0; max = 0;
    26     findMinMax(root, 0);
    27     int len = max - min + 1;
    28     output = new ArrayList[len];
    29     for(int i = 0; i < len; i ++){
    30         output[i] = new ArrayList<Integer>();
    31     }
    32     generate(root, 0);
    33     return output;
    34 }
    35 
    36 
    37 public static void main(String[] args) {    
    38     TreeNode root = new TreeNode(1);
    39     root.left = new TreeNode(2);
    40     root.right = new TreeNode(3);
    41     root.left.left = new TreeNode(4);
    42     root.left.right = new TreeNode(5);
    43     root.right.left = new TreeNode(6);
    44     root.right.right = new TreeNode(7);
    45     root.right.left.right = new TreeNode(8);
    46     root.right.right.right = new TreeNode(9);
    47     ArrayList[] result = verticalOrderTraveralBT(root);
    48     for(int i = 0; i < max - min + 1; i ++){
    49         System.out.println(result[i].toString());
    50     }
    51 }
  • 相关阅读:
    记一次笔试题
    模块模式浅析
    在windows上面实现ubuntu的三种方法
    移动端事件(五)—— 函数防抖和函数节流的封装
    一文分清强弱、动态静态类型语言
    为什么前端要学TypeScript
    OpenSSL SSL_read: Connection was reset, errno 10054
    浅悉http1.0 、http1.1和http2.0的区别
    上班一个月的总结
    从浅入深——理解JSONP的实现原理
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4338665.html
Copyright © 2011-2022 走看看