655. Print Binary Tree

来源:互联网 发布:在淘宝怎么买烟 编辑:程序博客网 时间:2024/05/16 14:44

题目

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    /   2Output:[["", "1", ""], ["2", "", ""]]

Example 2:

Input:     1    / \   2   3    \     4Output:[["", "", "", "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].

分析

首先计算二叉树高度得到矩阵行数,然后根据最多节点个数计算矩阵列数,声明一个全为空字符串的res[m][n]矩阵,然后根据不同高度下各节点所在区间取中间值得到该节点放置位置。

/** * 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:    void printBT(TreeNode* root,int beg,int end,int h,vector<vector<string>>& s){        s[h][(beg+end)/2]=to_string(root->val);//当前节点应放置在区间中间,利用(beg+end)/2找到索引位置        if(root->left!=NULL){            printBT(root->left,beg,(beg+end)/2-1,h+1,s);//分别构造左子树和右子树,增加层数        }        if(root->right!=NULL){            printBT(root->right,(beg+end)/2+1,end,h+1,s);        }        return ;    }    int heightBT(TreeNode* root){        if(root==NULL)            return 0;        int hl=heightBT(root->left)+1;        int hr=heightBT(root->right)+1;        return max(hl,hr);    }    vector<vector<string>> printTree(TreeNode* root) {        int height=heightBT(root);//首先计算二叉树的高度        int m=height,n=(1<<height)-1;//矩阵行数m等于二叉树高度,列数n等于2^m-1,利用位运算计算2^m次方        vector<vector<string>> res(m,vector<string>(n,""));//初始化结果矩阵,其中每行初始化为n个空字符串        res[0][n/2]=to_string(root->val);//对根节点进行构造,根节点在当前层数的区间的中间        if(root->left!=NULL){            printBT(root->left,0,n/2-1,1,res);//分别构造左子树和右子树,左子树在根节点左侧的区间,右子树在根节点右侧的区间        }        if(root->right!=NULL){            printBT(root->right,n/2+1,n-1,1,res);        }        return res;    }};


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 工商银行u盾忘记密码怎么办 民生银行不给u盾怎么办 银行不给开u盾怎么办 有车有空想赚点外快怎么办 车被注册了滴滴怎么办 快手号手机丢了怎么办 手机号码不停收到验证码怎么办 手机网页滑动自动跳到最下面怎么办 网页一打开就跳至评论怎么办? 百度总出现重复的网页怎么办 苹果7一直处于系统升级页面怎么办 手机看百度文章总是跳转怎么办 美度舵手滑丝怎么办 小区房产证办不下来怎么办 美海军陆战队进驻台湾大陆怎么办? 束脚裤带子怎么办系 眼角弄伤了应该怎么办 浴盆下水盖坏了怎么办 冒险岛勋章多了怎么办 玉手镯取不下来怎么办?终极绝招! 陆金所收益低怎么办 陆金所登录密码忘记怎么办 陆金所理财逾期怎么办 死亡家属晚上来闹怎么办 开车撞到人家属闹要请护工怎么办 骑马与砍杀战团打下城市怎么办 我的脑子有问题怎么办 公司外派异地工作医保怎么办 老鼠死在墙里面怎么办 父亲再婚婚后对我不好怎么办 皮衣搽了护理油后不亮了怎么办 新买的衣服皱了怎么办 新买的裙子很皱怎么办 新买的风衣很皱怎么办 新买的裙子皱了怎么办 货拉拉准点率低怎么办 定机票名字打错了怎么办 做坏事被发现了怎么办 在阳台做被发现怎么办 有秘密被发现了怎么办 微店没有收到货怎么办