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 ...
随机推荐
- springboot整合mybatis-plus基于纯注解实现一对一(一对多)查询
因为目前所用mybatis-plus版本为3.1.1,感觉是个半成品,所有在实体类上的注解只能支持单表,没有一对一和一对多关系映射,且该功能还在开发中,相信mybatis-plus开发团队在不久的将来 ...
- Redis07-Redis单节点容量问题,twemproxy,predixy的使用
Redis单节点容量问题 一.单节点容量问题 我们在实际场景中,往往遇上一个单节点容量问题. 1.进行业务拆分,数据分类 2.到了数据不能拆分的时候,可以进行数据分片 进行哈希取模(影响分布式下的扩展 ...
- checkbox 和 selected 混淆
1.option里面的selected属性,如果我们在下拉列表里面选择了一个option那么他的selected="true" ,如果我们想设置当前的option是选中 状态的, ...
- zencart分类页每页显示产品数量自定义选择的方法
zencart默认分类页每页显示产品数量是固定的,如何让顾客可以选择每页显示的产品的数量呢?效果图 方式一:全部展示 方式二:下拉菜单 修改方法 1.导入sql INSERT INTO configu ...
- HelloWorld编写过程中注意事项
一.package关键字 * package表示当前代码所属的包(package),是一种组织结构.其他package通过包名调用这个包下内容* package是必须的,每个文件的package必须存 ...
- MFC总结
1.首先是ListControl 简介: 列表视图控件List Control同样比较常见,它能够把任何字符串内容以列表的方式显示出来,这种显示方式的特点是整洁.直观,在实际应用中能为用户带来方便. ...
- CSS基础学习-5.CSS属性_字体文本文本装饰
- [uboot] (番外篇)uboot串口&console&stdio设备工作流程 (转)
[uboot] uboot流程系列:[project X] tiny210(s5pv210)上电启动流程(BL0-BL2)[project X] tiny210(s5pv210)从存储设备加载代码到D ...
- Spring的BeanPostProcessor和BeanFactoryPostProcessor区别
Spring提供了两种后处理bean的扩展接口,分别为BeanPostProcessor和BeanFactoryPostProcessor,这两者在使用上是有所区别的. BeanPostProcess ...
- puppet负载均衡之nginx+passenger
由于3.x系列已不再支持mongrel,所以就采用nginx+passenger来做负载均衡:之前有发过nginx+mongrel,puppet version是2.7系列的,所以还是可以用的: 环境 ...