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 一层一层打印出来

 package uberOnsite;

 import java.util.*;

 public class PrintTree {
public static class TreeNode {
char val;
int col;
TreeNode left;
TreeNode right;
public TreeNode(char value) {
this.val = value;
}
} static int colNum = 0; public static List<String> print(TreeNode root) {
List<String> res = new ArrayList<String>();
if (root == null) return res;
inorder(root);
levelOrder(root, res);
return res;
} public static void inorder(TreeNode node) {
if (node == null) return;
inorder(node.left);
node.col = colNum;
colNum++;
inorder(node.right); } public static void levelOrder(TreeNode node, List<String> res) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(node);
while (!queue.isEmpty()) {
StringBuilder line = new StringBuilder();
HashMap<Integer, Character> lineMap = new HashMap<Integer, Character>();
int maxCol = Integer.MIN_VALUE;
int size = queue.size();
for (int i=0; i<size; i++) {
TreeNode cur = queue.poll();
lineMap.put(cur.col, cur.val);
maxCol = Math.max(maxCol, cur.col);
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
for (int k=0; k<=maxCol; k++) {
if (lineMap.containsKey(k)) line.append(lineMap.get(k));
else line.append(' ');
}
res.add(line.toString());
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub PrintTree sol = new PrintTree(); TreeNode A = new TreeNode('a');
TreeNode B = new TreeNode('b');
TreeNode C = new TreeNode('c');
TreeNode D = new TreeNode('d');
TreeNode E = new TreeNode('e');
TreeNode F = new TreeNode('f');
TreeNode G = new TreeNode('g');
TreeNode H = new TreeNode('h');
TreeNode I = new TreeNode('i');
TreeNode J = new TreeNode('j');
TreeNode Z = new TreeNode('z'); A.left = B;
A.right = C;
B.left = D;
C.left = E;
C.right = F;
D.left = Z;
D.right = G;
E.right = H;
F.left = I;
F.right = J; List<String> res = print(A);
for (String each : res) {
System.out.println(each);
}
} }

U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column的更多相关文章

  1. LC 655. Print Binary Tree

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  2. [LeetCode] Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  3. [Swift]LeetCode655. 输出二叉树 | Print Binary Tree

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  4. LeetCode 655. Print Binary Tree (C++)

    题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...

  5. [LeetCode] 655. Print Binary Tree 打印二叉树

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  6. 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. leetcode_655. Print Binary Tree

    https://leetcode.com/problems/print-binary-tree/ 打印整棵二叉树 class Solution { public: int getTreeHeight( ...

  8. BFS广度优先 vs DFS深度优先 for Binary Tree

    https://www.geeksforgeeks.org/bfs-vs-dfs-binary-tree/ What are BFS and DFS for Binary Tree? A Tree i ...

  9. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

随机推荐

  1. BeautifulSoup总结

    1. 参考 Beautiful Soup 4.2.0 文档 Beautiful Soup Documentation (4.4.0 英文部分内容有别于4.2.0中文) CSS 选择器参考手册 阮一峰 ...

  2. numpy的array合并-【老鱼学numpy】

    概述 本节主要讲述如何把两个数组按照行或列进行合并. 按行进行上下合并 例如: import numpy as np a = np.array([1, 1, 1]) b = np.array([2, ...

  3. 在vue脚手架中使用npm的方式使用swiper

    打开项目的根目录,然后打开命令窗口,输入 npm install swiper@4.4.1 @后为指定版本号,也可以不写 在main.js 中,引入 import Swiper from 'swipe ...

  4. HTML入门8

    今天开始接触HTML里面的多媒体和嵌入内容 前面只讲了文字,下面来讲能够让网页动起来,更加有趣的嵌入元素,包含多媒体,包含图像的不同方式,以及怎样嵌入视频. HTML中图片,下面将深入使用它,以及&l ...

  5. 问题11:web前端开发规范手册(转)

    一.规范目的 1.1  概述 ..................................................................................... ...

  6. TCP三次握手四次挥手最通俗理解

    工作过程TCP标志位:TCP共有6个标志位,分别是: SYN(synchronous),建立联机.ACK(acknowledgement),确认.PSH(push),传输.FIN(finish),结束 ...

  7. Robot Framework 1

    about Robot, learnt from the following document, perfect document !!!! http://www.virtuousprogrammer ...

  8. ldd可执行程序时返回not a dynamic executable

    原因:32位程序放在64位机器上或64位程序放在32位程序上了 解决方法:如果是32位程序放在64位系统上则安装yum -y install libstdc++.i686,64位则是yum -y in ...

  9. X86-32位架构的CPU是不是内存只能到4G

    不是的,可以通过分页机制扩展实现超过4G内存的支持. 什么是分页机制扩展? PAE. 什么是PAE?   PAE如何实现的?  

  10. SNMP 优秀帖子

    -- 比较系统的描述http://blog.sina.com.cn/s/blog_54837cf301011607.html 几个SNMP官方网站(搜索关键字:snmplibrary C#):http ...