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 ...
随机推荐
- 03.DataStructure
01.list ''' list 특징 - 1차원 배열 구조 형식) 변수 = [값1, 값2] - 다양한 자료형 저장 가능 - index 사용=순서 존재 형식) 변수[n] - 값 수정( ...
- 易忘&有用 的冷门Anaconda命令
Python接触有将近2年了,但是在倒腾anaconda环境的时候,总有一些命令忘记需要查半天,今天趁着配置pytorch,特此总结一下,留待后用. 1. 创建和删除环境 创建环境 conda cre ...
- import文件时 ~/ 不识别问题(react)
在最近写的react的项目中,在webpack中配置的“~”可以定位到根路径,但是知道在同事在windows中跑程序时,发现怎么都不识别这个路径,所有相关文件都could not find modul ...
- CentOS 7 下安装 teamviewer 13
CentOS 版本:centos-release-7-4.1708.el7.centos.x86_64(通过 rpm -q centos-release 查询) teamviewer 版本:teamv ...
- Linux下提权常用小命令
有些新手朋友在拿到一个webshell后如果看到服务器是Linux或Unix操作系统的就直接放弃提权,认为Linux或Unix下的提权很难,不是大家能做的,其实Linux下的提权并没有很多人想象的那么 ...
- wpf1
emCombobox.Items[2].IsEnabled = false; 隐藏下拉框里面的一个item wpf 单例模式. [DllImport("user32", CharS ...
- Codeforces 1154 - A/B/C/D/E/F/G - (Undone)
链接:https://codeforces.com/contest/1154 A - Restoring Three Numbers - [水] #include<bits/stdc++.h&g ...
- Mybatis获取传参
取自 https://blog.csdn.net/weixin_38303684/article/details/78886375 mybatis中SQL接受的参数分为:(1)基本类型(2)对象(3 ...
- 香茅油:不只是驱虫剂 new
如果您是芳香疗法的爱好者,香茅油对您来说可能并不陌生.香茅油还经常被添加到各种个人护理和清洁产品中,给人们带来多种益处. 什么是香茅油? 香茅精油是从香茅属 (Cymbopogon ) 植物家族中提取 ...
- laradock
配置xdebug:http://m.php.cn/article/407434.html