【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 ...
随机推荐
- boost tuple
boost::tuple is a generalized version of std::pair. While std::pair can only store exactly two value ...
- 【优化】Java开发中注意内存问题,影响JVM
1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面: 第一,控制资源的使用,通过线程同步来控 ...
- [CSP-S模拟测试]:次芝麻(数学)
题目描述 小$K$和小$X$都是小次货.身为小次货,最重要的事情就是次啦!所以他们正在纠结如何分芝麻次.一开始,小$K$有$n$个芝麻,小$X$有$m$个芝麻.因为他们都想次更多芝麻,所以每次手中芝麻 ...
- LintCode之删除链表中的元素
题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...
- (转)Java并发包:AtomicBoolean和AtomicReference
转:https://blog.csdn.net/zxc123e/article/details/52057289 文章译自:http://tutorials.jenkov.com/java-util- ...
- soj#547 bzoj5046 分糖果游戏
分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long ; ][],s[],p[],v[]; si ...
- Asp.Net Core 第07局:路由
总目录 前言 本文介绍Asp.Net Core 路由. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:路由概述 1.路由主要用于处理特定的请求. ...
- 开启关闭mysql服务
1.Windows下 启动服务 mysqld --console 或 net start mysql 关闭服务 mysqladmin -uroot shudown 或 net stop mysql ...
- js中的attributes和Attribute的用法和区别。
一:Attribute的几种用法和含义(attributes和Attribute都是用来操作属性的) getAttribute:获取某一个属性的值: setAttribute:建立一个属性,并同时给属 ...
- vue中修改了数据但视图无法更新的情况(转)
原文地址:https://blog.csdn.net/qq_39985511/article/details/79778806