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 equal to the height of the given binary tree.
The column number n should always be an odd number.
The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them.
Each unused space should contain an empty string "".
Print the subtrees following the same rules.
Example :
Input: / Output:
[["", "", ""],
["", "", ""]]
Example :
Input: / \ \ Output:
[["", "", "", "", "", "", ""],
["", "", "", "", "", "", ""],
["", "", "", "", "", "", ""]]
Example :
Input: / \ / / Output: [["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Note: The height of binary tree is in the range of [, ].
Runtime: 4 ms, faster than 46.79% of C++ online submissions for Print Binary Tree.
可以看出,矩阵宽度是所有节点的和,长度是树高+1。然后就是二分,但要注意边界值。
class Solution {
public:
vector<vector<string>> printTree(TreeNode* root) {
int height = treedepth(root);
int arrlen = ( << (height+)) - ;
vector<vector<string>> ret(height+, vector<string>(arrlen, ""));
helper(root, ret, , , arrlen-);
return ret;
}
void helper(TreeNode* root, vector<vector<string>>& ret, int level, int start, int end){
if(!root) return ;
int idx = start + (end - start) / ;
ret[level][idx] = to_string(root->val);
helper(root->left, ret, level+, start, idx);
helper(root->right, ret, level+, idx+, end);
}
int treedepth(TreeNode* root){
if(!root) return -;
return + max(treedepth(root->left), treedepth(root->right));
}
};
LC 655. Print Binary Tree的更多相关文章
- 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] 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 ...
- 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: ...
- LC 971. Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- LC 965. Univalued Binary Tree
A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...
- LC 889. Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
随机推荐
- Java注解【三、注解的分类】
按运行机制分 源码注解 只在源码中存在 编译时注解 在class中依然存在,如@Deprecated 运行时注解 运行阶段起作用,如@Autowired 按来源分 JDK自带注解 三方注解 最常见 自 ...
- 1.RPC原理学习
1.什么是RPC:远程过程调用协议 RPC(Remote Procedure Call Protocol)— 远程过程调用协议,是一种通过网络从远程计算机程序上请求服务,而不需要 了解底层网络技术的协 ...
- ALIENTEK 战舰ENC28J60 LWIP和UIP补充例程(LWIP WEB有惊喜)
前面的话:自从接触网络模块,到现在有一阵子时间了,未来必定是网络的世界.学一些网络方面的知识是有必要的.我们ALINTEK 推出的ENC28J60网络模块块作为入门还是不错的.详细见此贴:http:/ ...
- TXNLP 33-40
词向量: 回顾基于检索的问答系统 倒排表解决:坑爹的翻译...应该翻译成“反向索引” 常规的索引是文档到关键词的映射: 文档——>关键词但是这样检索关键词的时候很费力,要一个文档一个文档的遍历一 ...
- @SpringQueryMap注解 feign的get传参方式(转)
spring cloud项目使用feign的时候都会发现一个问题,就是get方式无法解析对象参数.其实feign是支持对象传递的,但是得是Map形式,而且不能为空,与spring在机制上不兼容,因此无 ...
- 常见的SQL编写和优化
目录 常见SQL编写和优化 常见的SQL优化方式 常见SQL编写和优化 常见的SQL优化方式 对查询进行优化,应尽量避免全表扫描,首先应考虑在where及order by 涉及的列上建立索引. 应尽量 ...
- Python SMTP发送邮件Ⅱ
使用Python发送HTML格式的邮件 Python发送HTML格式的邮件与https://www.xuanhe.net/weixiu/4271.html发送纯文本消息的邮件不同之处就是将MIMETe ...
- 课时5:POW,POS,DPOS(矿工/矿机,工作证明)
比特币钱包
- yii项目连接多个数据库时, MySQL报错No such file or directory
服务器错误 yii\db\Exception SQLSTATE[HY000] [2002] No such file or directory ###'xxx是项目根目录' #0 xxx/vendor ...
- 51 Nod 1282 时钟 (循环中的最小表示+哈希)
1282 时钟 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 有N个时钟,每个时钟有M个指针,P个刻度.时钟是圆形 ...