1  public List<List<String>> printTree(TreeNode root) {
2 ArrayList<List<String>> al = new ArrayList<>();
3 if (root == null) return al;
4 if (root.left == null && root.right == null) {
5 al.add(new ArrayList<>());
6 al.get(0).add(String.valueOf(root.val));
7 return al;
8 }
9
10 int high = dep(root, 1);
11 String[][] ans = new String[(int) Math.pow(2, high)][high + 1];
12 add(root, (int) Math.pow(2, high - 1), 1, (int) Math.pow(2, high - 2), ans);
13 for (int i = 1; i <= high; i++) {
14 al.add(new ArrayList<>());
15 for (int j = 1; j < ans.length; j++)
16 al.get(i - 1).add(ans[j][i] == null ? "" : ans[j][i]);
17 }
18 return al;
19 }
20
21 public void add(TreeNode cur, int x, int y, int diff, String[][] ans) {
22 if (cur == null) return;
23 ans[x][y] = cur.val + "";
24 add(cur.left, x - diff, y + 1, diff / 2, ans);
25 add(cur.right, x + diff, y + 1, diff / 2, ans);
26 }
27
28
29 public int dep(TreeNode cur, int dep) {
30 if (cur == null) return dep - 1;
31 return Math.max(dep(cur.left, dep + 1), dep(cur.right, dep + 1));
32 }

leetcode 655. 输出二叉树 【时间击败100.00%】 【内存击败96.49%】的更多相关文章

  1. Java实现 LeetCode 655 输出二叉树(DFS+二分)

    655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...

  2. Leetcode 655.输出二叉树

    输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一行正中间. ...

  3. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  4. C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解

    C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...

  5. C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...

  7. [leetcode]1109. 航班预订统计(击败100%用户算法-差分数组的详解)

    执行用时2ms,击败100%用户 内存消耗52.1MB,击败91%用户 这也是我第一次用差分数组,之前从来没有碰到过,利用差分数组就是利用了差分数组在某一区间内同时加减情况,只会改变最左边和最右边+1 ...

  8. 力扣(LeetCode)单值二叉树 个人题解

    如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是单值二叉树时,才返回 true:否则返回 false. 示例 1: 输入:[1,1,1,1,1,null,1] 输出:tr ...

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

  10. LeetCode:翻转二叉树【226】

    LeetCode:翻转二叉树[226] 题目描述 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 题目 ...

随机推荐

  1. python 非阻塞线程对话框,非qt(解决qt MessageBox使用线程时候卡死问题)

    def msg_okbox(self, strinfo, isYesno=False): if isYesno: return win32api.MessageBox(None, strinfo, & ...

  2. chrome调试秘籍,让你的开发速度飞起来

    前言 熟练使用调试工具,势必能大幅提高我们的开发效率,达到事半功倍的效果.废话不多说,直接进入主题. Filter过滤 过滤器最简单的用法当然是直接输入过滤的字符,但这远远不够.除了直接输入,我们还希 ...

  3. 挂上burpsuite代理之后显示505 HTTP Version Not Supported 解决方法

    505 HTTP Version Not Supported  什么意思呢? HTTP505状态码代表的意思是 服务器不支持的HTTP版本,即 HTTP 505 HTTP Version Not Su ...

  4. 复习第6点-6.SpringMVC作用域传值

    作用域范围 对象名称 作用范围 application 整个作用范围 session 在当前会话中有效 request 在当前请求中有效 page 在当前页面有效 request/session/ap ...

  5. Kubernetes 安装网络插件(calico)

    简介 Calico是Kubernetes生态系统中另一种流行的网络选择.虽然Flannel被公认为是最简单的选择,但Calico以其性能.灵活性而闻名.Calico的功能更为全面,不仅提供主机和pod ...

  6. 创建maven项目时,IntelliJ IDEA2019出现:Unable to import maven project: See logs for details 报错

    开发环境:IntelliJ IDEA 2019.1.3 + Maven3.6.3 报错截图 主要原因 IntelliJ IDEA 2019.1.3 与 Maven3.6.3 不兼容问题 解决方案 将m ...

  7. Activity 的窗口去头的方式

    1\窗口去头的第一种方式 public class SplashActivity extends Activity { @Override protected void onCreate(Bundle ...

  8. linux 高并发系统限制 设置

    linux资源限制配置文件是/etc/security/limits.conf:限制用户进程的数量对于linux系统的稳定性非常重要. limits.conf文件限制着用户可以使用的最大文件数,最大线 ...

  9. 下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 'D:\python\demo\venv\Scripts\...的错误

    下载接口时出现:Try to run this command from the system terminal. Make sure that you use the correct version ...

  10. HCIP-进阶实验03-网络流量路径控制

    HCIP-进阶实验03-网络流量路径控制 实验需求 某城域网网络环境部署规划如图所示,该网络通过OSPF协议进行部署设计,分为四个区域,分别为骨干区域0.普通区域1.2.3.其中普通区域1为特殊区域N ...