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 ...
随机推荐
- Windows Server 2008 R2 修改远程桌面服务RDP默认端口及相应的防火墙配置
修改以下两个注册表项当中的默认端口3389为自定义端口: [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wd ...
- aspnet mvc 中 跨域请求的处理方法
ASP.NET 处理跨域的两种方式 方式1,后端程序处理.原理:给响应头加上允许的域即可,*表示允许所有的域 定义一个cors的过滤器 加在在action或者co ...
- flume进阶
上一张初识里面谢了一些flume入门的内容,其实在真正工作环境里面这种情况使用的是很少的,大部分情况,我们可能需要从多台设备的日志里面汇总收集数据并存储到HDFS上,以便于后期对数据进行处理,真实的情 ...
- 动态规划——Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 更新node和npm到最新版本
卸载 1.首先卸载nodejs,打开控制面板,然后找到程序卸载: 2.找到npm目录和npmcache 目录,直接删掉(一般情况下会在C:\Users\Caffrey\AppData\Roaming\ ...
- ProjectEuler && Rosecode && Mathmash做题记录
退役选手打发时间的PE计划 挂在这里主要是dalao们看到有什么想交流的东西可以私聊哦(站内信或邮箱吧) 2017/8/11 PE595 :第一题QAQ 2017/8/12 PE598 2017/ ...
- (85)Wangdao.com第十八天_JavaScript NodeList 接口,HTMLCollection 接口
NodeList 接口 HTMLCollection 接口 节点都是单个对象,有时需要一种数据结构,能够容纳多个节点 DOM 提供两种节点集合,用于容纳多个节点:NodeList 和 H ...
- Java 中的字符串与 []byte 字节数组
一.字符串 1.比较 String.HashSet.List 中的 contains 方法 其中, String.List 都使用了 indexOf 方法,本质是遍历,时间效率为 O(n).而 Has ...
- react路由
针对多个列表导航公用一个组建,然后 有两种路由方式 1.import {HashRouter as Router,Route,Link} from 'react-router-dom' 不过这个路由中 ...
- C语言面试题分类->宏定义
1.写一个“标准”宏,这个宏输入两个参数并返回较小的一个 答:#define MIN(x, y) ((x)<(y)?(x):(y))//注意x,y要加括号,因为x,y如果有复合运算会出现问题. ...