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. Django----admin模块初识

    步骤1:创建管理员用户:在Terminal输入:pythonmanage.py createsuperuser 如下图所示说明创建成功 步骤二:运行项目文件:python manage.py runs ...

  2. C++ vector的用法(整理)

    vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器.vector 是C++ STL的一个重要成员,使用它时需要包含头文件: #include<vector>; ...

  3. 阿里云负载均衡SSL证书配置(更新)

    阿里云负载均衡及应用防火墙SSL证书配置 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/8908461.html 好久了呢,距上篇博客的这段时间中:考试.搬家.工 ...

  4. MySql思维导图

  5. 请求库之selenium模块

    本片导航: 介绍及安装 基本使用 选择器 等待元素被加载 元素交互操作 其他及练习   一.介绍 1.简单概述 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无 ...

  6. (57)Wangdao.com第九天_JavaScript 对象的简介及分类

    对象 引用数据类型. 对象是一种复合的数据类型,可以存储多个不同类型的变量. 以后我们看到的,只要不是String.Number.Boolean.Null.Undefined,那么就一定是 Objec ...

  7. ECMA Script 6_RegExp 正则表达式

    在 ES5 中 RegExp 构造函数的参数有两种情况 RegExp(字符串, 正则表达式的修饰符) RegExp(正则表达式); var regex = new RegExp('xyz', 'i') ...

  8. [LeetCode] Implement Rand10() Using Rand7() 使用Rand7()来实现Rand10()

    Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...

  9. ES6 promise学习

    Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大. 1.promise是一构造函数,既然是构造函数,那么我们就可以用 new Promise()得到一个p ...

  10. elasticsearch视频34季

    02_结构化搜索_在案例中实战使用term filter来搜索数据 课程大纲 1.根据用户ID.是否隐藏.帖子ID.发帖日期来搜索帖子 (1)插入一些测试帖子数据 POST /forum/articl ...