leetcode_655. Print Binary Tree
https://leetcode.com/problems/print-binary-tree/
打印整棵二叉树
class Solution
{
public: int getTreeHeight(TreeNode* root)
{
return root==NULL?:max(getTreeHeight(root->left), getTreeHeight(root->right))+;
} vector<vector<string>> printTree(TreeNode* root)
{
int height = getTreeHeight(root);
vector<vector<string>> res(height, vector<string>((int)pow(, height)-,""));
dfs(root, , , pow(, height)-, res);
return res;
} void dfs(TreeNode* root, int layer, int l, int r, vector<vector<string>>& res)
{
if(root == NULL)
return;
int mid = (l+r)/;
stringstream ss;
string value;
ss<<root->val;
ss>>value;
res[layer][mid] = value;
dfs(root->left, layer+, l, mid-, res);
dfs(root->right, layer+, mid+, r, res);
}
};
leetcode_655. Print Binary Tree的更多相关文章
- 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 ...
- [LeetCode] Print Binary Tree 打印二叉树
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- [Swift]LeetCode655. 输出二叉树 | Print Binary Tree
Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...
- U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: ...
- 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 ...
- [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 ...
- 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- Print Nodes in Top View of Binary Tree
Top view of a binary tree is the set of nodes visible when the tree is viewed from the top. Given a ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
随机推荐
- Android Studio运行app时提示Error: Please select Android SDK
最近开启了一个android项目,点“Run”键运行app,并报错Error:Please select Android SDK: 选择 File -> Project Structure 调整 ...
- 在ubuntu18.0下安装qt4.7以及qt-creator安装过程中遇到的坑
最近的嵌入式Linux系统上要做课程设计= =要用贼老贼老的qt4.7,配环境踩坑都费了我1天时间.....所以记录下来,希望能给和我遇到相同问题的朋友一点帮助 apt-get install g++ ...
- docker registry的CI规划
目前代码全部署在docker中, 考虑用jenkins打包成docker包再推送到docker registry 打包推送过程中自动按照日期打标签,并且刷新latest
- 【SDOI 2011】染色
[题目链接] 点击打开链接 [算法] 树链剖分 [代码] 本题,笔者求最近公共祖先并没有用树链剖分“往上跳”的方式,而是用倍增法.笔者认为这样比较好写,代码可读性 比较高 此外,笔者的线段树并没有用懒 ...
- cookie绕过验证码登录
#coding:utf-8 ''' cookie绕过验证码登录,第一步先访问登录页面获取登录前的cookie,第二步用fiddler抓到的手动登录的cookie加入cookie中,登录成功,第三步登录 ...
- jsDoc 使用及配置!
原文地址:http://www.cnblogs.com/hxling/archive/2012/11/27/2791067.html jsDoc 说白了就是帮助你生成JS的文档,但有个前提,就是你编写 ...
- Pascal之计算小系统
program Project16; {$APPTYPE CONSOLE} VAR n,i,k,score,b,a:integer; answer,c:real; ch:char; Begin rep ...
- bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草【01背包】
在洛谷上被卡常了一个点! 就是裸的01背包咯 为啥我在刷水题啊 #include<iostream> #include<cstdio> #include<algorith ...
- 倒排索引构建算法BSBI和SPIMI
参考:https://blog.csdn.net/androidlushangderen/article/details/44889677 倒排索引 : 一般的索引检索信息的方式.比如原始的数据源假设 ...
- thunderbird 登录网易邮箱
登录密码不是自己的密码,而是在网易邮箱中设置的客户端授权ma,自己先进入邮箱进行设置即可