2018-07-27 15:55:13

问题描述:

问题求解:

题目中说明了最后的宽度计算其实是按照满二叉树来进行计算的,也就是说如果我们能够得到每层最左边的节点编号和最右边的节点编号,那么本题就可以进行解决了。

另外,在如何编号的问题上,既然是满二叉树,那么编号的方式自然是父节点i,左子节点2 * i,右子节点2 * i + 1。

    public int widthOfBinaryTree(TreeNode root) {
return helper(root, 0, 1, new ArrayList<Integer>(), new ArrayList<Integer>());
} private int helper(TreeNode root, int layer, int index, List<Integer> begin, List<Integer> end) {
if (root == null) return 0;
if (begin.size() == layer) {
begin.add(index);
end.add(index);
}
else end.set(layer, index);
int cur = end.get(layer) - begin.get(layer) + 1;
int l = helper(root.left, layer + 1, 2 * index, begin, end);
int r = helper(root.right, layer + 1, 2 * index + 1, begin, end);
return Math.max(cur, Math.max(l, r));
}

二叉树最大宽度 Maximum Width of Binary Tree的更多相关文章

  1. [Swift]LeetCode662. 二叉树最大宽度 | Maximum Width of Binary Tree

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  2. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  3. LC 662. Maximum Width of Binary Tree

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  4. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  5. [LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  6. [LeetCode] 662. Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  7. 662. Maximum Width of Binary Tree二叉树的最大宽度

    [抄题]: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

  8. 【leetcode】662. Maximum Width of Binary Tree

    题目如下: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

  9. [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. Centos下添加PHP对MSSQL的支持

    Leave a reply 其实很少会有连接SQL Server的机会,不过我们公司刚好有个应用需要使用的SQL Server的数据库,所以也知道给LNMP安装MSSQL的扩展. 搜索网上的相关文章一 ...

  2. innob and myisam存储引擎分析

    首次啊对比一下两者的区别: MyISAM   InnoDB   构成上的区别:   每个MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型. .frm文件存储表定义 ...

  3. css3实现头像旋转360度

    css样式: .div a img{ width: 88px; height: 88px; border-radius: 88px; transition: all 1.2s ease-out 0s; ...

  4. 生成带有表格的word附件和动态赋值

    生成带有表格的word附件和动态赋值, 表格前后需要添加一个特殊的开始和结束的域,表格第一列 TableStart:AuditJdcttbzInfo 和表格的最后一列 TableEnd:AuditJd ...

  5. 简单地说, cpp中的纯虚函数就是抽象类的具体实现

    简单地说, cpp中的纯虚函数就是抽象类的具体实现.包含了纯虚函数的类就是抽象类.

  6. MySQL中INSERT的一般用法

    原文链接:http://www.blogjava.net/midnightPigMan/archive/2014/12/15/421406.html MySQL中INSERT的一般用法 INSERT语 ...

  7. 20145304 Exp8 Web基础

    20145304 Exp8 Web基础 实验后回答问题 (1)什么是表单 表单用于搜集不同类型的用户输入,由三个基本组成部分表单标签.表单域.表单按钮.表单提交有两种方法,分别是get和post,使用 ...

  8. 20145312袁心《网络对抗》Web基础实践

    20145312袁心<网络对抗>Web基础实践 问题回答 1.什么是表单: 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程 ...

  9. 关于cp命令的编写

    关于cp命令的编写 娄老师在课上详细的讲了命令who的编写过程~对此,我很有启发!于是想亲自动手试试~ 有什么不足的地方请大家提出来! Learning by doing ~ 做中学,真的只有自己动手 ...

  10. SC命令---安装、开启、配置、关闭windows服务 bat批处理(转载)

    转载:http://www.jb51.net/article/49627.htm 转载:http://blog.csdn.net/c1520006273/article/details/5053905 ...