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 ...
随机推荐
- ABAP JSON
1 DATA: lv_em TYPE string, lv_em1 TYPE string. DATA: lt_but021 TYPE TABLE OF but021, lt_but0211 TYPE ...
- UVA11383 Golden Tiger Claw —— KM算法
题目链接:https://vjudge.net/problem/UVA-11383 题解: 根据KM()算法,标杆满足:l(x) + l(y) >= w(x, y) . 当求完最大权匹配之后,所 ...
- HDU2444 The Accomodation of Students —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2444 The Accomodation of Students Time Limit: 5000/1000 MS (Java ...
- How to Execute Page_Load() in Page's Base Class?
https://stackoverflow.com/questions/2737092/how-to-execute-page-load-in-pages-base-class We faced th ...
- .gitignore 使用入门
.gitignore /doc/ 过滤整个文件夹. *.zip 过滤所有.zip文件. /doc/info.txt 过滤某个具体的文件. 这样,push的时候,就不会上传了,git仓库中就没有了. 假 ...
- ThreadLocal工具类的使用(隔离思想)
ThreadLocal不是用来解决共享对象的多线程访问问题的, 通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象, ...
- 单纯形 BZOJ3112: [Zjoi2013]防守战线
题面自己上网查. 学了一下单纯形.当然 证明什么的 显然是没去学.不然估计就要残废了 上学期已经了解了 什么叫标准型. 听起来高大上 其实没什么 就是加入好多松弛变量+各种*(-1),使得最后成为一般 ...
- get与post提交方式区别?
1.get <!--表单数据作为HTTP GET请求发送给action 规定的URL,并将数据附加在URL之后,由客户端直接发送给服务器.表单数据不能太长,也不能含有非ASCII码的字符--&g ...
- bzoj 4236: JOIOJI【前缀和+map】
设sj,so,si分别是J O I的个数前缀和,然后要求求最长(l,r)满足sj[r]-sj[l-1]==so[r]-so[l-1]==si[r]-si[l-1],化简一下就是满足so[r]-so[l ...
- bzoj 1502 月下柠檬树【Simpson积分】
投影到地面之后,会发现圆形在平行光下面积和形状是不会变的,也就是所要求的图形是若干个圆和把相邻两个圆连起来的公切线所组成的. 公切线和圆间距瞎求一下就行,注意要去掉被完全覆盖的圆 然后simpson即 ...