题目如下:

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null.

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.

Example 1:

Input: 

           1
/ \
3 2
/ \ \
5 3 9 Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:

Input: 

          1
/
3
/ \
5 3 Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:

Input: 

          1
/ \
3 2
/
5 Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:

Input: 

          1
/ \
3 2
/ \
5 9
/ \
6 7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).

Note: Answer will in the range of 32-bit signed integer.

解题思路:对于一个二叉树,我们可以按层序遍历的顺序给每一个节点定义一个顺序索引,例如根节点是第一个遍历的节点,那么索引是1。很显然,根节点的左节点的索引是2,右节点是3。二叉树的父节点与左右子节点的索引满足这么一个规律的,如果父节点的索引值是i,那么左节点是2*i,右节点是2*i+1。所以,只需要用遍历二叉树,计算出每一层最左边的节点和最右边节点的索引的差值即可。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
dic = {}
res = 0
def traverse(self,node,level,inx):
if node == None:
return
if level not in self.dic:
self.dic[level] = [inx]
else:
if len(self.dic[level]) == 1:
self.dic[level].append(inx)
else:
self.dic[level][1] = inx
self.res = max(self.res,self.dic[level][1] - self.dic[level][0])
if node.left != None:
self.traverse(node.left,level + 1 ,inx*2)
if node.right != None:
self.traverse(node.right, level + 1, inx * 2 + 1) def widthOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.dic = {}
self.res = 0
self.traverse(root,0,1)
return self.res + 1

【leetcode】662. Maximum Width of Binary Tree的更多相关文章

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

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

  2. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  3. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  4. 【LeetCode】104 - Maximum Depth of Binary Tree

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

  5. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  6. 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 ...

  7. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  8. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

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

随机推荐

  1. JS中算法之排序算法

    1.基本排序算法 1.1.冒泡排序 它是最慢的排序算法之一. 1.不断比较相邻的两个元素,如果前一个比后一个大,则交换位置. 2.当比较完第一轮的时候最后一个元素应该是最大的一个. 3.按照步骤一的方 ...

  2. 用C#编写ActiveX控件

    http://www.cnblogs.com/homer/archive/2005/01/04/86473.html http://www.cnblogs.com/homer/archive/2005 ...

  3. slideshare原本是一个专业的幻灯片存储与展示的网站

    slideshare就是其中一个.slideshare原本是一个专业的幻灯片存储与展示的网站,它支持扩展名为ppt.pps和odp三种格式的幻灯片,用户上传成功以后slideshare会提供给用户一个 ...

  4. 【原】通过npm script运行webpack的原理

    原理:在项目中,局部安装依赖,依赖如果有创建命令的情况下会在node_modules/.bin目录创建软链接,pack.json默认读取到.bin下的命令. 如有理解不对,请各位大神纠正

  5. Linux查看硬件配置

    1.查看机器所有硬件信息:dmidecode |moredmesg |more 这2个命令出来的信息都非常多,所以建议后面使用"|more"便于查看 2.查看CPU信息 方法一: ...

  6. Python笔记(三)_字典与集合

    字典dict 映射类型,以键-值的方式存储,通过键来取相应的值 member={'one':1,'two':2,'three':3} 创建字典member=dict('苹果'='apple','桔子' ...

  7. java自学基础、项目实战网站推荐

    推荐一个自学的好平台,有Java前端,后端,基础的内容都有讲解,还有框架的讲解和实战项目,特别适合自学 JAVA 自学网站 JAVA 练习题 Mybatis 教程 Spring MVC 教程 模仿天猫 ...

  8. windows 安装多个mysql

    安装多个mysql,其实很简单,网上资料也很多,我整理一下,也跟着来凑个热闹. 1.下载mysql zip 解压到指定目录,我这边就3个,更多也类似 d:\mysql1 d:\mysql2 d:\my ...

  9. LeetCode6 dp

    120. Triangle 我的解法用了一个二维数组,这样比较浪费空间.O(n*n) 但是标准答案自底向上,一是不需要进行特别判断,二是可以覆盖数组,则只需要O(n)的空间大小. class Solu ...

  10. 超強的Linux指令解釋網站《explainshell.com》,學Linux必備!

    ExplainShell 官方網站:http://explainshell.com/ 原始碼下載:https://github.com/idank/explainshell 用瀏覽器打該explain ...