题目:

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. U盘安装咱中国人自己的操作系统UbuntuKylin14.04LST(超具体原创图文教程)

    本文仅供參考,在准备级安装过程中出现的一切意外情况均与本文作者无关!原创教程转载请注明原转载地!系统简单介绍:UbuntuKylin 是Ubuntu官方认可的衍生版,其宗旨是创建一个Ubuntu的中文 ...

  2. iOS 计算源码行数

    如果要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 1.列出每个文件的行数 find . -name "*.m" -or -name "*.h&qu ...

  3. spark练习--由IP得到所在地

    今天我们就来介绍,如何根据一个IP来求出这个IP所在的地址是什么,首先我们如果要做这个内容,那么我们要有一个IP地址的所在地字典,这个我们可以在网上购买,形如: 1.0.1.0|1.0.3.255|1 ...

  4. 【commons】IO工具类——commons-io之IOUtils

    本文转载自xingoo: https://www.cnblogs.com/xing901022/p/5978989.html 一.常用静态变量 public static final char DIR ...

  5. Linux下开发python django程序(Session读写)

    1.登陆设置session信息 def loginsession(req): if req.method == 'POST': loginform = LoginForm(req.POST) if l ...

  6. Linux下开发python django程序(设置admin后台管理上传文件和前台上传文件保存数据库)

    1.项目创建相关工作参考前面 2.在models.py文件中定义数据库结构 import django.db import modelsclass RegisterUser(models.Model) ...

  7. 26-[jQuery]-内容补充

    jquery除了咱们上面讲解的常用知识点之外,还有jquery 插件.jqueryUI知识点 jqueryUI 官网: https://jqueryui.com/ jqueryUI 中文网: http ...

  8. 【HNOI2015】亚瑟王

    题面 题解 考虑进行\(dp\). 设\(f[i][j]\)表示前\(i\)张卡中有\(j\)张被触发的概率. 我们可以知道第\(i\)张卡不被触发的概率为\((1 - p_i) ^ {r - j}\ ...

  9. CF 1114 C. Trailing Loves (or L'oeufs?)

    C. Trailing Loves (or L'oeufs?) 链接 题意: 问n!化成b进制后,末尾的0的个数. 分析: 考虑十进制的时候怎么求的,类比一下. 十进制转化b进制的过程中是不断mod ...

  10. 设计模式之module模式及其改进

    写在前面 编写易于维护的代码,其中最重要的方面就是能够找到代码中重复出现的主题并优化他们,这也是设计模式最有价值的地方 <head first设计模式>里有一篇文章,是说使用模式的心智, ...