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反射【三、方法的反射】
获取一个类下的所有方法 可以获取类类型后,获取到所有方法及相关信息 Method[] ms = c.getMethods(); 获取方法列表(public) Method[] ms = c.getDe ...
- Binlog_master
二进制日志 记录导致数据改变或潜在导致数据改变的SQL语句 记录已提交的日志 不依赖于存储引擎类型 功能:通过"重放"日志文件中的事件来生成数据副本 注意:建议二进制日志和数据文件 ...
- linux 命令技巧(转)--history
本文介绍一些关于bash的能够提高效率的技巧,主要是关于历史命令操作和一些快捷键,让你在命令行下工作效率翻倍. 1.history-----最基本的查看历史命令 2.!n-----编号为n的历史命令 ...
- 【异常记录(十)】 接口调用,提示跨域 Cross-domain calling interface, Access-Control-Allow-Origin
头的 Access-Control-Allow-Origin(允许访问的域) 改成 * : Response.AddHeader("Access-Control-Allow-Origin&q ...
- VUE-练习
作业一:有红黄蓝三个按钮,以及一个200*200矩形box,点击不同按钮,box的颜色会被切换为指定的颜色 <!DOCTYPE html> <html lang="en&q ...
- BBS-添加文章及文章中图片
目录 BBS项目中的添加文章 BBS项目中的添加文章中的图片 BBS项目中的添加文章 1.添加文章的时候,我们需要特别注意的是这个地方需要利用到到BeautifulSoup这个模块,因为我们在inpu ...
- .ko文件
ko文件介绍 .ko文件是kernel object文件(内核模块),该文件作用是把内核的部分功能移动到内核外边,需要的时候插入内核,不需要时卸载. 内核模块实现一些函数,作为回调函数注册到内核中.在 ...
- 抓取腾讯招聘python岗位
# -*- coding: utf-8 -*- """ @author: Dell Created on Mon Dec 23 17:55:06 2019 "& ...
- BZOJ 1107: [POI2007]驾驶考试egz / Luogu P3463 [POI2007]EGZ-Driving Exam (树状数组 LIS)
能从iii走到所有跑道 相当于 能从iii走到111和nnn. 边反向后就相当于 能从111和nnn走到iii. 为了方便叙述,把111~nnn叫做x坐标,111~(m+1)(m+1)(m+1)叫做y ...
- Flyway的简单介绍和使用(转)
Flyway的简单介绍及使用 一.开发时管理数据库遇到的问题: 现在开发一般都是团队开发,这样就会出现项目同步的问题,代码同步可以通过SVN工具管理起来,那数据库同步怎么办呢?理想的情况下,在开发新项 ...