Print a binary tree in an m*n 2D string array following these rules:

  1. The row number m should be equal to the height of the given binary tree.
  2. The column number n should always be an odd number.
  3. 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.
  4. Each unused space should contain an empty string "".
  5. 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;
}

最新文章

  1. 前端js文件合并三种方式
  2. javacomm64位用不了,可以使用RXTXcomm for x64
  3. 【CentOS】cp显示进度条
  4. Page.ClientScript.RegisterStartupScript不执行问题
  5. HTML5数据存储
  6. vue 数组渲染问题
  7. sql server中高并发情况下 同时执行select和update语句死锁问题 (二)
  8. Best Cow Line---POJ 3617(贪心)
  9. Emmet/Zen Coding 快速入门说明
  10. 怎样监听vue.js中v-for全部渲染完成?
  11. linux 进程监控软件 supervisor
  12. Nginx 错误日志配置
  13. 美国运营商推送假5G图标:用户当场蒙圈了
  14. MySql&#160;简单统计查询消耗时间脚本
  15. Up and Down the Tree CodeForces - 1065F (树形dp)
  16. python2.0_s12_day12_css样式详解
  17. 第7章 监听器Listener
  18. Extjs tree2
  19. 附件上传——mysql blob类型的数据(springboot)1
  20. check: 获得所有呗选中的checked标签的元素值 mapArrayElement(arrEles)

热门文章

  1. MVC5 数据注解和验证
  2. wubiuefi-支持新版本ubuntu的wubi
  3. jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)
  4. C++练习--实现客户机(CLIENT)类
  5. poj_3256_Cow Picnic
  6. php如何将base64数据流文件转换为图片文件?
  7. 微信小程序横向滚动
  8. react native 踩坑之 SectionList state更新 不执行render重新渲染页面
  9. js中的逻辑与和逻辑或随笔
  10. Ubuntu设置代理服务器