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 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 1:
Input:
1
/
2
Output:
[["", "1", ""],
["2", "", ""]]
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[["", "", "", "1", "", "", ""],
["", "2", "", "", "", "3", ""],
["", "", "4", "", "", "", ""]]
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output: [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Note: The height of binary tree is in the range of [1, 10].
分析:
给定一颗二叉树,按照格式打印这个树。
这道题的输出是一个二维数组,我们可以观察到,数组的高度等于树的高度h,宽度等于2^h-1,所以针对这道题,我们要先求出给定树的高度,再开辟相应大小的数组。
其次我们再观察,根节点处于二维数组第一行的中间,将左右两边均等的分开,其左右子树的根节点分别位于分开的两行的中间,可以想到使用递归来实现。
每次填充时传递一个高度参数用来锁定二维数组的行号。
程序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<string>> printTree(TreeNode* root) {
int h = getTreeH(root);
//w = 2^h-1
int w = (<<h)-;
vector<vector<string>> res(h, vector<string>(w));
printTree(root, res, , , w-);
return res;
}
void printTree(TreeNode* root, vector<vector<string>>& res, int h, int l, int r){
if(!root) return;
int mid = (l+r)/;
res[h][mid] = to_string(root->val);
printTree(root->left, res, h+, l, mid-);
printTree(root->right, res, h+, mid+, r);
}
//get tree height
int getTreeH(TreeNode* root){
if(!root) return ;
return max(getTreeH(root->left), getTreeH(root->right))+;
}
};
LeetCode 655. Print Binary Tree (C++)的更多相关文章
- [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 ...
- 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 equa ...
- 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- (转)sqlmap用户手册
原文地址:http://drops.wooyun.org/papers/143 http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlma ...
- [图解tensorflow源码] Simple Placer节点布放算法
- snapkit更新约束崩溃的问题
最近在使用snapkit布局时,竟然发现更新约束会导致崩溃,为什么这样呢? checkButton.snp.makeConstraints { (make) in make.left.top.equa ...
- 【OS X系统】Xcode中搭建Python环境。
虽然按照网上教程一步一步来,但还是遇到了几个错误点,现整理出来,主要是自己做个笔记,同时也希望能帮助到其他像我一样第一次在Xcode上搭建Python环境的人.首先感谢原作者:https://zhid ...
- A1084
输入:只能输入A-Z(不区分大小写),0-9和下划线: 第一行输入应输入字符串,第二行输入实际输入字符串. 输出:按大写输出缺少的字符,每个字符输出一次. 注意: 1.由于不区分大小写,则需要将小写字 ...
- 学习笔记一:I2C协议学习和Verilog实现
////////////////////////////////////////////////// //clk = 20 MHz ,一个周期50ns //sck = 100 kHz (scl) ,一 ...
- 【转】如何内网搭建NuGet服务器
原文:http://www.cnblogs.com/zhangweizhong/p/7755332.html NuGet 是.NET程序员熟知的工具,它可以直接安装开源社区中的各个公用组件,可以说是非 ...
- mssqlserver分区表的左值与右值
参考官方文档: https://docs.microsoft.com/zh-cn/sql/t-sql/statements/create-partition-function-transact-sql ...
- # 2017-2018-1 20155224 《信息安全系系统设计基础》第四周MyOD
2017-2018-1 20155224 <信息安全系系统设计基础>第四周MyOD 在这里跟老师先道歉,当时我的git没有安装好,后面也一直没有装上,所以程序没有git. 要求 参考教材第 ...
- JavaWeb总结(七)
Web状态管理 - HTTP协议使用的是无状态的连接 - 对容器而言,每一个请求都来自于一个新的客户 解决方案-表单隐藏字段 <input type=”hidden” name=”session ...