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. 002 python准备做题的一些准备

    在这里,刷刷题,然后,将比较有用的连接粘贴一下在这里.

  2. 理解ActivityManagerService

    --摘自<Android进阶解密> *AMS家族* 1.Android 7.0的AMS家族 2.AMP和AMS进行通信 3.(Android 7.0)AMP是AMN的内部类,它们都实现了I ...

  3. 第三篇 request篇

    每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...

  4. oracle 删除重复记录

    查询及删除重复记录的SQL语句   1.查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断   select * from 表 where Id in (select Id from 表 g ...

  5. 潭州课堂25班:Ph201805201 tornado 项目 第五课 增加用户系统-用户中心(课堂笔记)

    tornado 相关说明 在 users 表中创建记录,做测试 在项目根目录下创建 test.py # -*- coding: utf-8 -*- # 斌彬电脑 # @Time : 2019/2/27 ...

  6. Android第四次作业

    一.团队成员 成员1:刘宇莹 学号:1600802122 班级:计算机164 博客链接:刘宇莹 成员2:孟鑫菲 学号:1600802092 班级:计算机163 博客链接:孟鑫菲 二.团队项目apk 拍 ...

  7. Snackbar 提醒

    类似于Toast,属于design包,不要忘记导入design package com.xiaozhuyisheng.jinjiedemos.activity; import android.supp ...

  8. maven和glassfish安装和部署及hello1和hello2的部署

    1.安装maven和glassfish及配置环境变 首先搜索并下载maven3.6.0和glassfish4.1.1(版本看按需要选择). 点击安装包进行安装 安装完成后开始配置环境变量 打开系统环境 ...

  9. Android手机camera和IMU的标定

    https://qingsimon.github.io/post/2018-12-28-android%E6%89%8B%E6%9C%BA%E7%9B%B8camera%E5%92%8Cimu%E7% ...

  10. Jenkins调度Selenium脚本不能打开浏览器解决办法

    前提:在Myeclipse里面可以启动起来浏览器,在Jenkins中不能启动浏览器 原因:以程序的方式安装了jenkins,jenkins就成了windows的一个服务了,默认是设置为自动启动的如下图 ...