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. [转]VS中展开和折叠代码

    VS2005代码编辑器的展开和折叠代码确实很方便和实用.以下是展开代码和折叠代码所用到的快捷键,很常用: Ctrl + M + O: 折叠所有方法 Ctrl + M + M: 折叠或者展开当前方法 C ...

  2. SQL查询遍历数据方法一 [ 临时表 + While循环]

    以下以SQL Server 2000中的NorthWind数据库中的Customers表为例, 用 临时表 + While循环 的方法, 对Customers表中的CompanyName列进行遍历 c ...

  3. uva12206 后缀数组

    这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用 利用后缀数组计算最长的lcp 这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 ...

  4. 【工具使用】Git密码存储相关问题探究以及资料整理

    在公司的托管平台gogs上,遇到一个任务需要用不同的账号进行操作和处理.这样就遇到一个问题了,死活没有办法在拉去代码的时候,提示输入用户,输入密码. 我的操作系统是mac.安装了git环境,用的软件是 ...

  5. vc编辑器常用设置

    代码格式化 1.选中代码: 2.ctrl+K: 3.ctrl+F; 显示行号

  6. Bootloader之uBoot简介

    本文转载自:http://blog.ednchina.com/hhuwxf/1915416/message.aspx 一.Bootloader的引入 从前面的硬件实验可以知道,系统上电之后,需要一段程 ...

  7. 打开vi后提示The ycmd server SHUT DOWN (restart with :YcmRestartServer)该如何处理

    答:进入YouCompleteMe的安装目录安装一些必要的依赖 比如:笔者将YouCompleteMe安装到了~/.vim/bundle目录下,那么执行以下操作: cd ~/.vim/bundle/Y ...

  8. ubuntu14.04禁止触摸板和恢复触摸板

    1.使用xinput list查看与触摸板相关的id,以下是本机的输出,没搞清楚为什么是Mouse!!! jello@jello:~$ xinput list⎡ Virtual core pointe ...

  9. 再谈vim中多窗口的编辑 ctrl+w+H窗口位置最大化和互换等操作

    参考:http://blog.csdn.net/shuangde800/article/details/11430659 很好 鼠标在各个窗口间循环移动: ctrl+w+(小写的 hjkl), &qu ...

  10. win7系统远程桌面无法正常连接

    我的电脑--属性--远程设置:初步设置: 此外还需要确认服务是否开启