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. oracle安装登录sqlplus / as sysdba然后报错ERROR: ORA-01031 insufficient privileges

    解决办法: 一般情况下检查操作系统的登录用户是否包含在ORA_DBA组中. 控制面板->管理工具->计算机管理->系统工具->本地用户和组->ORA_DBA组. 如果OR ...

  2. POJ3186 Treats for the Cows —— DP

    题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K To ...

  3. 工作中常用到的JS校验

    1. // 验证是否为空 2. function check_blank(obj, obj_name){ 3. if(obj.value != ''){ 4. return true; 5. }els ...

  4. hdu 2553(N皇后问题)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. HDU6012:Lotus and Horticulture(扫描线)(前缀和)

    传送门 题意 给出n种植物的适宜培育温度区间[l,r],低于下限获值c,高于上限获值b,其他获值a 分析 首先考虑应该尝试选择哪些点:区间的左右端点.与区间左右端点距离0.50.5的点,这样就一定可以 ...

  6. SAS基础 -- 逻辑库不存在问题解决

    SAS安装 -- 逻辑库不存在问题解决 1.SAS打不开,将系统时间改为系统提示错误的时间: 2.请在以下两个路径下,新建SASCFG文件夹(注意红色部分为你电脑SAS的安装路径)   D:\Prog ...

  7. bzoj 3677: [Apio2014]连珠线【树形dp】

    参考:http://www.cnblogs.com/mmlz/p/4456547.html 枚举根,然后做树形dp,设f[i][1]为i是蓝线中点(蓝线一定是父子孙三代),f[i][0]为不是,转移很 ...

  8. P4323 [JSOI2016]独特的树叶(树哈希)

    传送门 树哈希?->这里 反正大概就是乱搞--的吧-- //minamoto #include<bits/stdc++.h> #define R register #define l ...

  9. 洛谷P3287 [SCOI2014]方伯伯的玉米田(树状数组)

    传送门 首先要发现,每一次选择拔高的区间都必须包含最右边的端点 为什么呢?因为如果拔高了一段区间,那么这段区间对于它的左边是更优的,对它的右边会更劣,所以我们每一次选的区间都得包含最右边的端点 我们枚 ...

  10. Luogu P1273 有限电视网【树形Dp/树形背包】

    题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...