• U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column


    Give a binary tree, elegantly print it so that no two tree nodes share the same column. 
    
    Requirement: left child should appear on the left column of root, and right child should appear on the right of root.
    
    Example: 
        a
       b   c
     d   e   f
    z g   h i j

    这道题若能发现inorder traversal each node的顺序其实就是column number递增的顺序,那么就成功了一大半

    维护一个global variable,colNum, 做inorder traversal

    然后level order 一层一层打印出来

     1 package uberOnsite;
     2 
     3 import java.util.*;
     4 
     5 public class PrintTree {
     6     public static class TreeNode {
     7         char val;
     8         int col;
     9         TreeNode left;
    10         TreeNode right;
    11         public TreeNode(char value) {
    12             this.val = value;
    13         }
    14     }
    15     
    16     static int colNum = 0;
    17     
    18     public static List<String> print(TreeNode root) {
    19         List<String> res = new ArrayList<String>();
    20         if (root == null) return res;
    21         inorder(root);
    22         levelOrder(root, res);
    23         return res;
    24     }
    25     
    26     public static void inorder(TreeNode node) {
    27         if (node == null) return;
    28         inorder(node.left);
    29         node.col = colNum;
    30         colNum++;
    31         inorder(node.right);
    32         
    33     }
    34     
    35     public static void levelOrder(TreeNode node, List<String> res) {
    36         Queue<TreeNode> queue = new LinkedList<TreeNode>();
    37         queue.offer(node);
    38         while (!queue.isEmpty()) {
    39             StringBuilder line = new StringBuilder();
    40             HashMap<Integer, Character> lineMap = new HashMap<Integer, Character>();
    41             int maxCol = Integer.MIN_VALUE;
    42             int size = queue.size();
    43             for (int i=0; i<size; i++) {
    44                 TreeNode cur = queue.poll();
    45                 lineMap.put(cur.col, cur.val);
    46                 maxCol = Math.max(maxCol, cur.col);
    47                 if (cur.left != null) queue.offer(cur.left);
    48                 if (cur.right != null) queue.offer(cur.right);
    49             }
    50             for (int k=0; k<=maxCol; k++) {
    51                 if (lineMap.containsKey(k)) line.append(lineMap.get(k));
    52                 else line.append(' ');
    53             }
    54             res.add(line.toString());
    55         }
    56     }
    57     
    58     /**
    59      * @param args
    60      */
    61     public static void main(String[] args) {
    62         // TODO Auto-generated method stub
    63         
    64         PrintTree sol = new PrintTree();
    65         
    66         TreeNode A = new TreeNode('a');
    67         TreeNode B = new TreeNode('b');
    68         TreeNode C = new TreeNode('c');
    69         TreeNode D = new TreeNode('d');
    70         TreeNode E = new TreeNode('e');
    71         TreeNode F = new TreeNode('f');
    72         TreeNode G = new TreeNode('g');
    73         TreeNode H = new TreeNode('h');
    74         TreeNode I = new TreeNode('i');
    75         TreeNode J = new TreeNode('j');
    76         TreeNode Z = new TreeNode('z');
    77         
    78         A.left = B;
    79         A.right = C;
    80         B.left = D;
    81         C.left = E;
    82         C.right = F;
    83         D.left = Z;
    84         D.right = G;
    85         E.right = H;
    86         F.left = I;
    87         F.right = J;
    88         
    89         List<String> res = print(A);
    90         for (String each : res) {
    91             System.out.println(each);
    92         }
    93     }
    94 
    95 }
  • 相关阅读:
    requests库简单介绍与使用
    python爬虫之无界面谷歌浏览器介绍
    PhantomJS介绍与使用
    使用find_elements_by_class_name定位元素有时候会出现打印出来的列表里面数据为空的现象,解决方案
    【C#】多态
    【JavaScrpt】JS之数组去重
    【SQL】sql语句在insert一条记录后返回该记录的ID
    【SQL】SQL整表复制
    【C#】获取URL上的参数
    【C#】 break continue return 的区别
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6367347.html
Copyright © 2020-2023  润新知