题目:

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-1int w = (1<<h)-1;vector<vector<string>> res(h, vector<string>(w));printTree(root, res, 0, 0, w-1);return res;}void printTree(TreeNode* root, vector<vector<string>>& res, int h, int l, int r){if(!root) return;int mid = (l+r)/2;res[h][mid] = to_string(root->val);printTree(root->left, res, h+1, l, mid-1);printTree(root->right, res, h+1, mid+1, r);     }//get tree heightint getTreeH(TreeNode* root){if(!root) return 0;return max(getTreeH(root->left), getTreeH(root->right))+1;}
};

转载于:https://www.cnblogs.com/silentteller/p/10721048.html

LeetCode 655. Print Binary Tree (C++)相关推荐

  1. 655. Print Binary Tree 解题报告(树)

    第一部分:搜索.遍历 [例子1]655. Print Binary Tree Example 1: Input:1/2 Output: [["", "1", & ...

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

  3. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  4. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  5. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal

    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal Solution1:我的答案 仿照105题写的答案 / ...

  6. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...

  7. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

  8. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

  9. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

最新文章

  1. mongodb 安装部署说明
  2. Kindle:自动追更之云上之旅
  3. thinkphp中配置信息的二维数组设置与使用
  4. Persist Security Info 参数的作用
  5. Ui5 tool debug - ctrl+alt+shift+s实现原理
  6. 深入浅出根据函数调用过程谈栈回溯原理
  7. 被定制化开发坑惨了的IT人,不是所有的报表都能满足老板的需求
  8. 提高篇 第三部分 图论 第4章 差分约束系统
  9. 吴恩达深度学习 —— 4.2 深层网络中的前向传播
  10. python解析与输出emoji
  11. html中字体 楷体_css怎么设置字体为楷体?
  12. NAS网络配置、资源管理和用户访问权限
  13. cuda 的driver API 和 runtime API
  14. 截图工具因为计算机无法使用,win7系统截图工具无法使用提示“当前未在计算机上运行”的解决方法...
  15. 计算机技术包括云计算吗,云计算机技术有哪些
  16. VINS-Mono之后端非线性优化 (目标函数中视觉残差和IMU残差,及其对状态量的雅克比矩阵、协方差递推方程的推导)
  17. Python经典练习题——求水仙花数
  18. 一个微服务业务系统的中台构建之路
  19. spring-boog-测试打桩-Mockito
  20. leetcode *974. 和可被 K 整除的子数组(2020.5.27)

热门文章

  1. linux php 入侵,入侵linux教程 - 冷漠 ' Blog - Focus on Information Security !
  2. base64编码_动画演示 Base 64 编码
  3. 华为南太无线解决方案部梁旭阳_华为“测温”手机申请了专利,华为今年共申请了 8,607 项无线专利...
  4. 全网最详细的Android Studio卸载、安装和启动教程
  5. python写sql语句_python3将变量写入SQL语句的实现方式
  6. 小米笔记本bios版本大全_初级用户应该学习的BIOS知识
  7. android 移除泛型中元素_Android 代码混淆 混淆方案
  8. linux与Windows查看路由
  9. python课程设计的主要任务是什么_Python网络编程课程设计 任务1.docx
  10. python写错了如何撤销-Python集成开发工具Pycharm的使用方法:复制,撤销上一步.......