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 ...
随机推荐
- Web自动化测试中的接口测试
1.2.3 接口可测性分析 接口显而易见要比UI简单的都,只需要知道协议和参数即可完成一次请求,从自动化测试实施难易程度来看,有以下几个特征: 1)驱动执行接口的自动化成本不高:HTTP,RPC,SO ...
- centos7中的网卡名称相关知识
转载自https://www.cnblogs.com/zyd112/p/8143464.html 一致性网络设备命名(Consistent Network Device Naming) 背景介绍: 在 ...
- win10电脑配置
微信 QQ 电脑管家 Chrome 坚果云 Sublime VLC 网易云音乐 Acrobat Reader DC PS git potplayer TeamViewer 有道云笔记/协作 百度网盘/ ...
- Centos7下rc.local文件开机不执行…
在Centos7下,rc.local文件,开机默认是不执行的 /etc/rc.local链接的是 /etc/rc.d/rc.local文件,该文件默认不开机启动,官方建议添加一个service来实现 ...
- PAT Basic 1056 组合数的和 (15 分)
给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出:25.28.52.58.82.85 ...
- 网络协议相关面试问题-https加密算法相关面试问题
密钥: 密钥是一种参数,它是在使用密码cipher算法过程中输入的参数,同一个明文在相同的密码算法和不同的密钥计算下会产生不同的密文.所以说算法既使公开了但是密钥参数不同其结果也是不同的,其中关于明文 ...
- PAT乙级1018
题目链接 https://pintia.cn/problem-sets/994805260223102976/problems/994805304020025344 题解 刚开始做很懵逼,可能并不难吧 ...
- javaWeb中的session和cookie
Cookie Cookie 是浏览器提供的一种技术,通过服务器的程序能将一些只须保存在客户端,或者 在客户端进行处理的数据,放在本地的计算机上,不需要通过网络传输,因而提高网页处理的效率,并且能够减少 ...
- Web Api(4)
参考原文链接https://www.cnblogs.com/JamelAr/,本文大部分内容是根据这位博主进行实验测试的,非常感谢分享,另外也参考了https://www.cnblogs.com/vi ...
- 微信小程序aes前后端加密解密交互
aes前后端加密解密交互 小程序端 1. 首先引入aes.js /** * [description] CryptoJS v3.1.2 * [description] zhuangzhudada so ...