[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 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].
思路:
观察例子可以发现,行m就是树的高度h,列n就是2h-1,那么首先构造一个二维数组ret[m][n].用“”初始化。
观察得到,每一个深度为d的树节点t都在ret所在的行为d(假设下标从1开始)的中间。而t的左孩子,则深度为t+1,所在的行下标范围[left,mid-1]
可以递归。
int maxDepth(TreeNode *root)
{
int depth;
if(root==NULL)return ;
else{
depth=(maxDepth(root->left)>maxDepth(root->right)?
maxDepth(root->left):maxDepth(root->right))+;
return depth;
}
}
void printTree(vector<vector<string>>& ret,TreeNode* root,int leftIndex,int rightIndex,int curDepth,int maxDepth)
{
if(curDepth>maxDepth || root==NULL||leftIndex>rightIndex) return;
int mid = (leftIndex+rightIndex)/;
ret[curDepth-][mid] = to_string(root->val);
printTree(ret,root->left,leftIndex,mid-,curDepth+,maxDepth);
printTree(ret,root->right,mid+,rightIndex,curDepth+,maxDepth);
}
vector<vector<string>> printTree(TreeNode* root)
{
int depth = maxDepth(root);
if(depth == ) return {{}}; vector<vector<string>> ret(depth,vector<string>(pow(,depth)-,"")); printTree(ret,root,,pow(,depth)--,,depth);
return ret;
}
[leetcode-655-Print Binary Tree]的更多相关文章
- [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 (C++)
题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...
- 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 ...
随机推荐
- 学习Promise笔记
什么是Promise? MDN对Promise的定义:Promise对象用于异步操作,它表示一个尚未完成且预计在未来完成的异步操作. 在学习Promise之前得先了解同步与异步:JavaScript的 ...
- LinkedList---链表各种方法的实现
public class ListExer2 { public static void main(String[] args) { LinkList list = new LinkList(); li ...
- 路由器基本配置实验,静态路由和动态RIP路由
实验涉及命令以及知识补充 连线 PC和交换机FastEtherNet接口 交换机和路由器FastEtherNet接口 路由器和路由器Serial接口 serial是串行口,一般用于连接设备,不能连接电 ...
- Linux下通过uptime判断负载情况
[root@localhost ~]# uptime 18:34:12 up 5:30, 1 user, load average: 0.00, 0.01, 0.05 load average后面的三 ...
- DevOps - 版本控制 - Bitbucket
Bitbucket 使用139邮箱无法收到注册邮件 https://bitbucket.org
- web学习第二天
今天是学习web的第二天,早上用css3做了个会动的小熊,border-radius为圆的半径, .smallxiong { width: 400px; height: 400px; ...
- mysql日志管理#二进制日志详解
查看MySQL二进制文件中的内容有两种方式 mysqlbinlog SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row ...
- Scrapy框架的初步使用
Scrapy scrapy框架是一个非常全面的爬虫框架,可以说是爬虫界的django了,里面有相当多的组件,格式化组件item,持久化组件pipeline,爬虫组件spider 首先我们要先和djan ...
- struts2架构网站漏洞修复详情与利用漏洞修复方案
struts2从开发出来到现在,很多互联网企业,公司,平台都在使用apache struts2系统来开发网站,以及应用系统,这几年来因为使用较多,被攻击者挖掘出来的struts2漏洞也越来越,从最一开 ...
- C语言实例解析精粹学习笔记——35(报数游戏)
实例35: 设由n个人站成一圈,分别被编号1,2,3,4,……,n.第一个人从1开始报数,每报数位m的人被从圈中推测,其后的人再次从1开始报数,重复上述过程,直至所有人都从圈中退出. 实例解析: 用链 ...