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

Posted neverlandly

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 

以上是关于U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column的主要内容,如果未能解决你的问题,请参考以下文章

Groupon面经Prepare: Max Cycle Length

FB面经 Prepare: Make Parentheses valid

M面经Prepare: Find integer Average of 2 integers.

FB面经Prepare: Merge K sorted Array

G面经Prepare: Longest All One Substring

FB面经 Prepare: K closest point to the origin