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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. [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 ...

  5. [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 ...

  6. 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: ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. MySQL数据库笔记五:多表查询

    1.表与表之间的关系 一对一:用户表和身份信息表,用户表是主表 例如:男人表 .女人表 create table man( mid int primary key auto_increment, mn ...

  2. redis-cluster集群总结

    Redis集群搭建 要想搭建一个最简单的Redis集群,那么至少需要6个节点:3个Master和3个Slave.为什么需要3个Master呢?如果你了解过Hadoop/Storm/Zookeeper这 ...

  3. Mysql(三)-1:存储引擎

    一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处理文本用txt类型 ...

  4. unzip解压3G或者4G以上文件失败的解决方法

    Linux下,使用unzip解压时,报错:End-of-central-directory signature not found.  Either this file is nota zipfile ...

  5. (七)zabbix监控nginx

    1.agent端配置 1)nginx编译安装需要加上该选项--with-http_stub_status_module 2)修改nginx配置文件 #vim /usr/local/nginx/conf ...

  6. HDFS知识点

    1.通过代码验证集群的配置文件的优先级 HDFS文件上传 1.编写源代码 @Test public void testCopyFromLocalFile() throws IOException, I ...

  7. 【Python网络】HTTP

    HTTP概述 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器和万维网服务器之间互相通信的规则. HTTP就是一个通信规则,通信规则规定 ...

  8. 通过远程 HTTP GET 请求载入信息

    jQuery.get(url, [data], [callback], [type]) 概述 通过远程 HTTP GET 请求载入信息. 这是一个简单的 GET 请求功能以取代复杂 $.ajax .请 ...

  9. spring-boot的三种启动方式

    spring-boot的启动方式主要有三种: 1. 运行带有main方法类 2. 通过命令行 java -jar 的方式 3. 通过spring-boot-plugin的方式 一.执行带有main方法 ...

  10. JavaMail应用--通过javamail API实现在代码中发送邮件功能

    JavaMail应用   在日常开发中,可能会引用到发邮件功能,例如在持续集成中,自动化测试运行完毕,自动将测试结果以报表的形式发送邮件给相关人.那么在Java中如何实现发邮件呢? 在java EE ...