博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
655. Print Binary Tree
阅读量:6277 次
发布时间:2019-06-22

本文共 2560 字,大约阅读时间需要 8 分钟。

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", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]
/** * 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
> printTree(TreeNode* root) { int m = get_height(root); int n = 1; for(int i =2;i<=m;i++) { n = 2*n+1; } vector
> res (m,vector
(n,"")); dfs(root,res,0,0,n); return res; }private: void dfs(TreeNode*root,vector
> &res,int depth,int start,int end) { if(!root) return; if(start>end) return; int mid = (start+end)/2; res[depth][mid] = to_string(root->val); dfs(root->left,res,depth+1,start,mid-1); dfs(root->right,res,depth+1,mid+1,end); } int get_height(TreeNode*root) { if(!root) return 0; return 1+max(get_height(root->left),get_height(root->right)); }};

 

转载于:https://www.cnblogs.com/jxr041100/p/8041075.html

你可能感兴趣的文章
MOSS系列二 创建第一个SharePoint站点
查看>>
Detach Volume 操作 - 每天5分钟玩转 OpenStack(55)
查看>>
MySQL5.6 部署MHA
查看>>
DG配置网络,报ORA-12514: TNS:listener does not...
查看>>
hadoop开启webHDFS服务及测试
查看>>
DC学院学习笔记(十七):分类及逻辑回归
查看>>
Spring Aop(一)——Aop简介
查看>>
document.createElement
查看>>
Outlook Anywhere 客户端配置详解
查看>>
Go语言学习资料整理
查看>>
精进不休 .NET 4.0 (3) - asp.net 4.0 新特性之动态数据(Dynamic Data)增强
查看>>
麻将游戏
查看>>
用“ICET”轻松诊断 Windows 7 网络连接高级功能
查看>>
在MPAndroidChart库K线图的基础上画均线
查看>>
Gradle 1.12用户指南翻译——第四十四章. 分发插件
查看>>
查询远程或本地计算机的登录账户
查看>>
chk cloud
查看>>
asp.net事件顺序
查看>>
即时数据模块设计 版本V2
查看>>
CCNP-6 OSPF试验2(BSCI)
查看>>