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

这道题给了我们一棵二叉树,让我们以数组的形式打印出来。数组每一行的宽度是二叉树的最底层数所能有的最多结点数,存在的结点需要填入到正确的位置上。那么这道题我们就应该首先要确定返回数组的宽度,由于宽度跟数组的深度有关,所以我们首先应该算出二叉树的最大深度,直接写一个子函数返回这个最大深度,从而计算出宽度。下面就是要遍历二叉树从而在数组中加入结点值。我们先来看第一行,由于根结点只有一个,所以第一行只需要插入一个数字,不管这一行多少个位置,我们都是在最中间的位置插入结点值。下面来看第二行,我们仔细观察可以发现,如果我们将这一行分为左右两部分,那么插入的位置还是在每一部分的中间位置,这样我们只要能确定分成的部分的左右边界位置,就知道插入结点的位置了,所以应该是使用分治法的思路。在递归函数中,如果当前node不存在或者当前深度超过了最大深度直接返回,否则就给中间位置赋值为结点值,然后对于左子结点,范围是左边界到中间位置,调用递归函数,注意当前深度加1;同理对于右子结点,范围是中间位置加1到右边界,调用递归函数,注意当前深度加1,参见代码如下:

解法一:

class Solution {
public:
vector<vector<string>> printTree(TreeNode* root) {
int h = getHeight(root), w = pow(, h) - ;
vector<vector<string>> res(h, vector<string>(w, ""));
helper(root, , w - , , h, res);
return res;
}
void helper(TreeNode* node, int i, int j, int curH, int height, vector<vector<string>>& res) {
if (!node || curH == height) return;
res[curH][(i + j) / ] = to_string(node->val);
helper(node->left, i, (i + j) / , curH + , height, res);
helper(node->right, (i + j) / + , j, curH + , height, res);
}
int getHeight(TreeNode* node) {
if (!node) return ;
return + max(getHeight(node->left), getHeight(node->right));
}
};

下面这种方法是层序遍历二叉树,使用了两个辅助队列来做,思路都一样,只不过是迭代的写法而已,关键还是在于左右边界的处理上,参见代码如下:

解法二:

class Solution {
public:
vector<vector<string>> printTree(TreeNode* root) {
int h = getHeight(root), w = pow(, h) - , curH = -;
vector<vector<string>> res(h, vector<string>(w, ""));
queue<TreeNode*> q{{root}};
queue<pair<int, int>> idxQ{{{, w - }}};
while (!q.empty()) {
int n = q.size();
++curH;
for (int i = ; i < n; ++i) {
auto t = q.front(); q.pop();
auto idx = idxQ.front(); idxQ.pop();
if (!t) continue;
int left = idx.first, right = idx.second;
int mid = left + (right - left) / ;
res[curH][mid] = to_string(t->val);
q.push(t->left);
q.push(t->right);
idxQ.push({left, mid});
idxQ.push({mid + , right});
}
}
return res;
}
int getHeight(TreeNode* node) {
if (!node) return ;
return + max(getHeight(node->left), getHeight(node->right));
}
};

参考资料:

https://discuss.leetcode.com/topic/98381/java-recursive-solution

https://discuss.leetcode.com/topic/98503/java-iterative-level-order-traversal-with-queue

LeetCode All in One 题目讲解汇总(持续更新中...)

最新文章

  1. 【Telerik】查询控件&lt;telerik:RadMaskedTextBox&gt;的使用
  2. Zybo GPIO Demo Run Embedded Linux
  3. Linux简介及常用命令使用3--vi编辑器
  4. android service 的各种用法(IPC、AIDL)
  5. 如何判断一个变量是否是utf-8
  6. IOS彩票第一天基本框架搭建
  7. 编译android源码官方教程(6)编译内核
  8. 迅美VPS安装和配置MySQL数据库教程
  9. MVC ueditor的使用(实现上传图片功能)
  10. input hidden用法
  11. ubuntu修改主机名称
  12. sed替换文件中的字符串
  13. Linux搭建SVN 服务器(转)
  14. HTTP使用BASIC认证的原理及实现方法(还有NTLM方法,比较复杂)
  15. 使用 sphinx 制作简洁而又美观的文档
  16. 【Android 应用开发】Android - TabHost 选项卡功能用法详解
  17. 电子产品使用感受之--DJI OSMO Pocket VS OSMO MOBILE
  18. windows中的软链接硬链接等
  19. error LNK1169 找到一个或多个多重定义的符号的解决方法
  20. [P5172] Sum

热门文章

  1. Linux中SVN的备份与恢复
  2. 解决NSURLConnection finished with error - code -1100错误
  3. Go实现海量日志收集系统(二)
  4. 事后诸葛亮分析——Beta版本
  5. 团队作业7——第二次项目冲刺(Beta版本12.04)
  6. iOS极光推送SDK的使用流程
  7. Android属性动画 nineoldandroids
  8. 搭建java环境——使用Sublime Text 3(windows环境)
  9. 前端之bootstrap模态框
  10. SpringMVC源码情操陶冶#task-executor解析器