【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 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的更多相关文章
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【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 ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- 【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 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【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 ...
- [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 ...
随机推荐
- GO富集分析 信号通路
基因富集分析是分析基因表达信息的一种方法,富集是指将基因按照先验知识,也就是基因组注释信息进行分类. 信号通路是指能将细胞外的分子信号经细胞膜传入细胞内发挥效应的一系列酶促反应通路.这些细胞外的分子信 ...
- 【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
题目如下: In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pair ...
- python基础知识(1)(个人整理)
import文件夹下的py文件: 情况1: `-- src |-- mod1.py `-- test1.py 直接 import mod1.py即可 情况2: -- src |-- mod ...
- Mybatis的运行原理
我们写的sql语句就在statementBuilder中. 整个mapper的信息都保存到configuration 1:根据配置文件创建SQLSessionFactory 先创建SqlSession ...
- springmvc的@ModelAttribute
1:作用:执行任何方法前都要先执行一下有这个标识的方法. 用途:表单回显的时候先从数据库中查询出来放到这个方法中. 1):模拟表单 <form action="first/testMo ...
- RabbitMQ的简单应用【转】
虽然后台使用了读写分离技术,能够在一定程度上抗击高并发,但是如果并发量特别巨大时,主数据库不能同时处理高并发的请求,这时数据库容易宕机. 问题: 现在的问题是如何既能保证数据库正常运行,又能实现用户数 ...
- vue之条件语句小结
vue之条件语句小结 v-if, v-else 随机生成一个数字,判断是否大于0.5,然后输出对应信息: <!DOCTYPE html> <html> <head> ...
- mysql审计插件
Audit Plugin安装使用 原文: https://www.cnblogs.com/waynechou/p/mysql_audit.html#_label0 #有卸载方法 下载地址: htt ...
- codeforces gym100418J
题目简述 给定N 求1到N中有多少个幸运数字 幸运数字的定义为 这个数能被它二进制表示下1的个数整除 其中(1 ≤ N ≤ 1019) -------------------------------- ...
- Python 进阶_闭包 & 装饰器
目录 目录 闭包 函数的实质和属性 闭包有什么好处 小结 装饰器 更加深入的看看装饰器的执行过程 带参数的装饰器 装饰器的叠加 小结 装饰器能解决什么问题 小结 闭包 Closure: 如果内层函数引 ...