【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 ...
随机推荐
- Quartz.Net 任务调度之时间策略(4)
在复杂的业务逻辑中,cron可以灵活的制定时间策略 先说使用方法 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trig ...
- JavaScript-黑科技
单行写一个评级 var rate = 3; "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate); 随机字符串 Math.random().toStrin ...
- java获取当月日期 和 周末
/** * java获取 当月所有的日期集合 * @return */public static List<Date> getDayListOfMonth() { List list = ...
- Juint test Case 的2种使用方式
通常情况下,我们去测试一个类中的方法,首先是建立一个包,包中建立一个测试类,在建立测试类文件时,选择JUnit Test Case,如下: 建好之后写测试用例: 但是如果偏就想在编写方法的那个java ...
- LUOGU P3380 【模板】二逼平衡树(树套树)
传送门 解题思路 这里写的是常数巨大的线段树套\(splay\),卡了半天常才过.首先线段树每个节点挂一个\(splay\),\(splay\)中的元素即为线段树管辖的区间中的数.对于操作\(1\), ...
- ibatis 的使用
1. 文本的使用 select ‘day’+Num from Table;//Sql select convert(varchar,'day')+Num from Table;//ibatis
- 重写ArcGIS的TiledMapServiceLayer调用天地图瓦片
require(["esri/layers/TiledMapServiceLayer"], function () { dojo.declare("com.StrongI ...
- GPIO 的 8 种工作模式
GPIO 的 8 种工作模式 在初始化 GPIO 的时候,根据我们的使用要求,必须把 GPIO 设置为相应的模式.如 LED 例程中的 GPIO 引脚如果配置为模拟输入模式是必然会导致错误的. 我们配 ...
- 【转】 Linux 命令解释(Linux基础二)
前言 对服务器来讲,图形界面会占用更多的系统资源,而且会安装更多的服务.开放更多的端口,这对服务器的稳定性和安全性都有负面影响.其实,服务器是一个连显示器都没有的家伙,要图形界面干十么? 说到这里,有 ...
- mongo 数据库存储
mongo 数据库,获取有赞的数据. from app import mongo from app.external.yz.goods_api import YzGoodsApi from openp ...