题目:

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:

  1. Input:
  2. 1
  3. /
  4. 2
  5. Output:
  6. [["", "1", ""],
  7. ["2", "", ""]]

Example 2:

  1. Input:
  2. 1
  3. / \
  4. 2 3
  5. \
  6. 4
  7. Output:
  8. [["", "", "", "1", "", "", ""],
  9. ["", "2", "", "", "", "3", ""],
  10. ["", "", "4", "", "", "", ""]]

Example 3:

  1. Input:
  2. 1
  3. / \
  4. 2 5
  5. /
  6. 3
  7. /
  8. 4
  9. Output:
  10.  
  11. [["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
  12. ["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
  13. ["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
  14. ["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]

Note: The height of binary tree is in the range of [1, 10].

分析:

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

这道题的输出是一个二维数组,我们可以观察到,数组的高度等于树的高度h,宽度等于2^h-1,所以针对这道题,我们要先求出给定树的高度,再开辟相应大小的数组。

其次我们再观察,根节点处于二维数组第一行的中间,将左右两边均等的分开,其左右子树的根节点分别位于分开的两行的中间,可以想到使用递归来实现。

每次填充时传递一个高度参数用来锁定二维数组的行号。

程序:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. vector<vector<string>> printTree(TreeNode* root) {
  13. int h = getTreeH(root);
  14. //w = 2^h-1
  15. int w = (<<h)-;
  16. vector<vector<string>> res(h, vector<string>(w));
  17. printTree(root, res, , , w-);
  18. return res;
  19. }
  20. void printTree(TreeNode* root, vector<vector<string>>& res, int h, int l, int r){
  21. if(!root) return;
  22. int mid = (l+r)/;
  23. res[h][mid] = to_string(root->val);
  24. printTree(root->left, res, h+, l, mid-);
  25. printTree(root->right, res, h+, mid+, r);
  26. }
  27. //get tree height
  28. int getTreeH(TreeNode* root){
  29. if(!root) return ;
  30. return max(getTreeH(root->left), getTreeH(root->right))+;
  31. }
  32. };

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. 谈谈我的js学习过程(二)——“Hello World!”

    在<谈谈我的js学习过程(一)>中,我简单聊了一下我认为的javascript的学习方法,接下来我们可以尝试来写一个最简单的js代码. "Hello World!"对于 ...

  2. CentOS如何配置yum源

    参考:http://blog.csdn.net/qingfenggege/article/details/80394564 1. yum 前端软件包管理器2. 基于RMP包管理,能够从指定的服务器自动 ...

  3. Mac开发中遇到的一些小问题解析

    通过mac开发的过程中,有一些小问题出现,列如下,后续会持续增加: 1. 命令行清空废纸篓(jar包太多,倾倒废纸篓太慢) sudo rm -rfv ~/.Trash /Volumes/*/.Tras ...

  4. Hadoop(16)-MapReduce框架原理-自定义FileInputFormat

    1. 需求 将多个小文件合并成一个SequenceFile文件(SequenceFile文件是Hadoop用来存储二进制形式的key-value对的文件格式),SequenceFile里面存储着多个文 ...

  5. d3 js emberjs handlerbarjs

    http://handlebarsjs.com/ http://emberjs.com/ http://jsbin.com/d3ember-barchart/13/edit?html,output

  6. python字典键值对转化为相应的变量名和变量值

    将python字典键值对转化为相应的变量名和变量值可以使用以下方法: globals().update({"name":"value"}) locals().u ...

  7. Scribe+HDFS日志收集系统安装方法

    1.概述 Scribe是facebook开源的日志收集系统,可用于搜索引擎中进行大规模日志分析处理.其通常与Hadoop结合使用,scribe用于向HDFS中push日志,而Hadoop通过MapRe ...

  8. UOJ UR#9 App管理器

    题目传送门 题目大意大概就是给你一个混合图(既有有向边又有无向边),对于每条无向边,u-v,问删去u->v,或删去v->u那条可以使新图强连通.(保证数据有解). 这道题前几个数据点送分. ...

  9. python基础学习1-字典的使用

    id_db={1:"wh" ,2:"wx" ,3:{1:"a",2:"b",3:"c"} ,4:[& ...

  10. 4-[多进程]-互斥锁、Queue队列、生产者消费者

    1.互斥锁 (1)为什么需要互斥锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 而共享带来的是竞争,竞争带来的结果就是错乱,如下 #并发运行,效率 ...