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 一层一层打印出来
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- leetcode_655. Print Binary Tree
https://leetcode.com/problems/print-binary-tree/ 打印整棵二叉树 class Solution { public: int getTreeHeight( ...
- 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 ...
- 将百分制转换为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 ...
随机推荐
- 多台CentOS服务器下实现SSH免密码登录
ROOT用户下实现SSH免密码登录 第一步:进入目录/root/.ssh $ cd /root/.ssh/ 执行以下命令,会在当前目录下生成公钥(id_rsa.pub)/私钥(id_rsa)对 第二 ...
- H5唤醒app,不完全兼容
---ps---最近新发现一个开源的H5唤醒app的库:建议使用第三方开源库https://github.com/suanmei/callapp-lib实现:或者极光魔链(后期可能会收费)https: ...
- 权限管理——shiro
1.shiro整合spring a.导入依赖 <properties> <shiro.version>1.2.4</shiro.version> </prop ...
- zoj3781
zoj3781赛场上堵在了缩点上emmmmm把原始图相同颜色的方块缩成一个点,然后与它周围不同颜色的联通块连双向边,然后枚举每个点然后求最大深度的最小值因为每次翻转都相当于深度+1(可以手动模拟一下 ...
- 用户管理和su,id 命令
useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...
- 【C#】多数组间的取重取余
string[] arrRate = new string[] { "a", "b", "c", "d" };//A s ...
- SQL语句删除表中的字段只留下最新一行
方法一 DELETE FROM A WHERE `name` in ( SELECT a.name FROM( SELECT name FROM A a GROUP BY name HAVING CO ...
- Python学习笔记整理(python 3)
一.tuple(元组) tuple和list非常类似,但是tuple一旦初始化就不能修改,如: classmates = ('Michael', 'Bob', 'Tracy') 1 classmate ...
- python MRO:C3算法
http://www.codeweblog.com/python-mro-c3%E7%AE%97%E6%B3%95/ 在 python 2.2 之后,python 实现了一个新的MRO算法:C3算法, ...
- Mac下GTest的基本使用
Mac下GTest的基本使用 gtest全称Google C++ Testing Framework,它是从谷歌内部诞生并受到业界追捧的一个非常优秀的测试框架,支持如自动发现测试.自定义断言.死亡测试 ...