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

思路:

观察例子可以发现,行m就是树的高度h,列n就是2h-1,那么首先构造一个二维数组ret[m][n].用“”初始化。

观察得到,每一个深度为d的树节点t都在ret所在的行为d(假设下标从1开始)的中间。而t的左孩子,则深度为t+1,所在的行下标范围[left,mid-1]

可以递归。

int maxDepth(TreeNode *root)
{
int depth;
if(root==NULL)return ;
else{
depth=(maxDepth(root->left)>maxDepth(root->right)?
maxDepth(root->left):maxDepth(root->right))+;
return depth;
}
}
void printTree(vector<vector<string>>& ret,TreeNode* root,int leftIndex,int rightIndex,int curDepth,int maxDepth)
{
if(curDepth>maxDepth || root==NULL||leftIndex>rightIndex) return;
int mid = (leftIndex+rightIndex)/;
ret[curDepth-][mid] = to_string(root->val);
printTree(ret,root->left,leftIndex,mid-,curDepth+,maxDepth);
printTree(ret,root->right,mid+,rightIndex,curDepth+,maxDepth);
}
vector<vector<string>> printTree(TreeNode* root)
{
int depth = maxDepth(root);
if(depth == ) return {{}}; vector<vector<string>> ret(depth,vector<string>(pow(,depth)-,"")); printTree(ret,root,,pow(,depth)--,,depth);
return ret;
}

[leetcode-655-Print Binary Tree]的更多相关文章

  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. LeetCode 655. Print Binary Tree (C++)

    题目: Print a binary tree in an m*n 2D string array following these rules: The row number m should be ...

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

  4. 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  5. [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 ...

  6. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  7. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  10. (二叉树 递归) 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. Promise面试题

    题目一 const promise = new Promise((resolve, reject) => { console.log(1); resolve(); console.log(2); ...

  2. 由inline-block小例子引申出的一些问题,及IE6、IE7兼容性解决方案

    使用场景分析: 常见的对块与块之间的横向排列处理 对同级所有元素使用display:inline-block; , 之后块与块直接会产生间隙问题 解决办法: 给父级设 font-size:0; 别高兴 ...

  3. 简析--UUID

    内容转载自:http://www.cnblogs.com/java-class/p/4727698.html 阅读目录 1.UUID简介 2.UUID组成 3.UUID实战演练 1.UUID 简介 U ...

  4. ajax 全局拦载处理,可加密、过滤、筛选、sql防注入处理

    //此方法放在公用的js里面即可.如此:所有的ajax请求都会通过此 $.ajaxSetup({ contentType: "application/x-www-form-urlencode ...

  5. Oracle PL/SQL中异常高级特性

    在OraclePL/SQL语句块中exception的异常处理部分是非常重要的组成部分,它决定了在PL/SQL语句块内部可执行部分在发生异常错误时,程序是友好地提示:程序遇到某些错误而无法执行,还是抛 ...

  6. 如何安全地跨窗体调用Timer控件 从一个窗体调用控制另外一个窗体的控件

    具体的情况是Form1中有一个Timer2时钟,Timer2时钟事件弹出Warning窗体,点击Warning窗体上面的按钮,重新激活一下Form1中的Timer2.从而实现了从一个窗体调用另外一个窗 ...

  7. 编译安装开源免费中文分词scws

    一.SCWS了解一下: SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统). 这是一套基于词频词典的机械式中文分词引擎,它能将一整段的 ...

  8. ubuntu以root进入图形化界面

    sudo nautilus 可以进行一些文件夹移动操作,不会出现权限的问题

  9. js,setTimeout与setInterval的用法

    1.setTimeout与setInterval的区别 setTimeout: 1.直接使用的话,按照指定 的时间,只执行一次传入的函数参数. 2.函数的终止使用clearTimeout. setIn ...

  10. IDEA无法引入已经创建的类

    解决办法: 清理一下缓存: File->Invalidate Caches/Restart... OVER!