题目:

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

分析:

给定一颗二叉树,按照格式打印这个树。

这道题的输出是一个二维数组,我们可以观察到,数组的高度等于树的高度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++)的更多相关文章

  1. [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 ...

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

  3. 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. [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 ...

  5. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  9. (二叉树 递归) 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 ...

随机推荐

  1. sqoop执行job报错(org/json/JSONObject)

    NoClassDefFoundError: org/json/JSONObject: [root@hadoop2 ~]# sqoop job --create myjob7 --  import -- ...

  2. Windows安装TensorFlow遇到错误

    1.先检查系统是64还是32位的,检查python版本是否相符合 2.windows系统上使用tensorflow需要python3.5版本

  3. docker~dockertoolbox的加速器

    对于在win10以下的操作系统上跑docker,我们可以安装docker toolbox工具,下载安装后第一次启动它会从远程github上下载最新版的boot2docker镜像文件,40多兆,但下载非 ...

  4. Cloudera Manager Server CDH 5.15部署

    安装前准备 主机和系统 Host OS Memory IP bigdata001-dev Cent OS 7.4 x64 32G 192.168.1.1 bigdata002-dev Cent OS ...

  5. 高德地图API(流程法)整理分析

    [高德地图API(流程法)分析]: 前言:公司现在的网约车项目,使用的是高德地图,因为地图导航这一块的功能占比量比较大,为了方便大家对高德地图API的了解和学习使用,使用流程图把高德API分析整理了下 ...

  6. Go语言学习 总结一

    1,定义main, package main 一个可独立执行的程序,(类似main方法) 2,import “fmt” fmt 实现格式化IO(输入/输出) (类似system.out.print() ...

  7. Dotnet Core Cli 解决方案中多个项目的相互引用和第三方库引用

    dotnet add app/app.csproj reference lib/lib.csproj app项目引用lib项目 dotnet add package Newtonsoft.Json 当 ...

  8. WPF字体模糊解决方案

    原文:WPF字体模糊解决方案 WPF对字体渲染做了很大的改善,与Winform窗体字体相比较,更加平滑,但是当字体大小较小时,则会出现字体模糊的现象.可通过以下方法进行改善处理: 对于XAML用户界面 ...

  9. 单元测试时 出现找不到类或者 NoClassDefFoundError 的问题

    这种情况下,启动或重启下服务器即可

  10. 【oracle查询】oracle查询字段显示#号 (井号)

    客户反映字段查询为井号,我自己没有遇到这种情况,于是上网百度了一下. 下面的答案很好地解决了问题,哈哈哈.