二叉树最大宽度 Maximum Width of Binary Tree
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的更多相关文章
- [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 ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 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 ...
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- [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 ...
- [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 ...
- 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 ...
- 【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 ...
- [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 ...
随机推荐
- Fiddler过滤指定域名
Fiddler过滤指定域名的方法一 切换到fiddler右侧窗口的Filters选项卡,勾选顶部的“Use Filters”,找到Hosts区域,设置以下三个选项: 1.第一项有三个选项,不做更改: ...
- JSP—作用域
application: 用于同一个应用内,所有用户之间的数据共享 作用域: request作用域: 在页面转发,包含中同样有效. <% pageContext.include("te ...
- 能让程序做的事情坚决不用人来做——批量修复markdownlint MD034警告
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- SVN Error: Unreadable path encountered; access denied;
最近在公司弄了版本库.将主代码丢到版本库后,想拉取新的分支.抛异常如下: SVN Error: Unreadable path encountered; access denied; 解决办法: 1. ...
- 【转】svn http://提示svn: Unrecognized URL scheme错误
转自:http://blog.csdn.net/l241002209/article/details/8547943 SVN这个东西虽然不好用(当然也是因为自身没有重视,但是谁让GIT这么好用呢…!) ...
- Linux基础命令---cmp
cmp 用字节的方式,比较两个文件是否存在差异,但是不保存运算结果.Cmp指令只会根据结果设置相关的标志位,这个指令之后往往会跟着一个条件跳转指令. 此命令的适用范围:RedHat.RHEL.Ubun ...
- MySQL用sql复制表数据到新表的方法
用sqlyog无法直接复制出一个不同表名的表来,只能copy到其他库上同名的表. 在MySQL数据库中,应该如何用sql将表数据复制到新表中呢? 本人通过试验测试成功了,而且相当简单易懂,速度也非常快 ...
- [转载]DropDownList三级菜单联动源码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- TED #09# You don't have to be an expert to solve big problems
Tapiwa Chiwewe: You don't have to be an expert to solve big problems Collection noticed a haze hangi ...
- 简单的HTML5 canvas游戏工作原理
HTML5已经不是一个新名词.它看上去很cool,有很多feature,大多数人普遍看好它的发展.对于我来说,最感兴趣的是它的canvas标签,可以结合Javascript来绘制游戏画面. 我们可以在 ...