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的更多相关文章

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

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

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

  4. 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: ...

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

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

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

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

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

  9. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

随机推荐

  1. Swift入门(十)——循环引用、弱引用和无主引用

    近期看到swift里面不仅有循环引用和弱引用(weak),还加入了无主引用(unowned),于是写了一些demo,这里总结一下. 和OC一样.Swfit默认也是基于ARC进行内存管理的,因此尽管简单 ...

  2. YTU 2980: 几点了

    2980: 几点了 时间限制: 1 Sec  内存限制: 128 MB 提交: 37  解决: 9 题目描述 现有一个Time类可以用来记录时间,请输出Time记录的时间加上s秒后的时间. 只需提交补 ...

  3. Javascript 解析字符串生成 XML DOM 对象。

    Javascript 接收字符串生成 XML DOM 对象.实测对 Firefox .IE6 有效.可用于解析 ajax 的服务器响应结果,也可用于解析自定义字符串.​1. [代码]函数   ppt模 ...

  4. 【转】Java并发编程:Synchronized底层优化(偏向锁、轻量级锁)

     一.重量级锁 上篇文章中向大家介绍了Synchronized的用法及其实现的原理.现在我们应该知道,Synchronized是通过对象内部的一个叫做监视器锁(monitor)来实现的.但是监视器锁本 ...

  5. Linux服务器性能问题

    如何快速分析出现性能问题的Linux服务器 https://www.cnblogs.com/leixiaobai/category/1246164.html Brendan Gregg曾经分享过当遇到 ...

  6. 【135】NoteExpress使用中的问题

    NoteExpress主要是用来管理文献,然后可以方便管理,方便插入,各种方便吧! 关于NoteExpress的下载可以直接搜索进入官网下载,为了不用破解之类的,可以选择大学版的! 引文显示上标!ht ...

  7. [Swift通天遁地]一、超级工具-(3)带切换图标的密码文本框

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. web项目tomcat启动url自定义(去掉项目名)

    通常,使用maven构建web项目,启动时默认的访问路径: http://ip:port/项目名 很多时候我们不喜欢这样 访问,我们希望下面的访问方式: http://ip:port 如果是本地的to ...

  9. iOS导航栏NavigationBar的颜色,按钮和标题以及字体颜色

    首先,层级关系: leftBarButtonItem.rightBarButtonItem.title都是加在UINavigationItem上的,UINavigationItem再加在Navigat ...

  10. MoveTo和LineTo函数的意思

    这是个画线函数, moveto是移动到某个坐标,lineto是从当前坐标, 移动的某个坐标连接早当前坐标.这两个函数加起来就是画一条直线.