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

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/maximum-width-of-binary-tree/description/

题目描述:

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.

题目大意

给定二叉树,求二叉树的最大宽度。二叉树某层的宽度是指其最左非空节点与最右非空节点之间的跨度。

解题方法

做法是层次遍历 + 完全二叉树的节点位置性质。这个性质指的是,每层都有 2 ^ (n-1)个节点。某节点的左孩子的标号是2n, 右节点的标号是2n + 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):
def widthOfBinaryTree(self, root):
"""
:type root: TreeNode
:rtype: int
"""
queue = collections.deque()
queue.append((root, 1))
res = 0
while queue:
width = queue[-1][1] - queue[0][1] + 1
res = max(width, res)
for _ in range(len(queue)):
n, c = queue.popleft()
if n.left: queue.append((n.left, c * 2))
if n.right: queue.append((n.right, c * 2 + 1))
return res

日期

2018 年 3 月 21 日 ———— 啊,耽误了一天没刷题。。

【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)的更多相关文章

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

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

  3. [LeetCode]662. Maximum Width of Binary Tree判断树的宽度

    public int widthOfBinaryTree(TreeNode root) { /* 层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1 而且要有两个变量,一个记录本层节点数, ...

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

  5. 【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 ...

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

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

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

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

  9. 662. Maximum Width of Binary Tree

    https://leetcode.com/problems/maximum-width-of-binary-tree/description/ /** * Definition for a binar ...

随机推荐

  1. 在windows下使用shell,运行shell脚本

    在Windows操作系统下运行Shell脚本,缺少的只是一个Git软件.其下载路径为Git - Downloading Package. 安装之后,将安装路径下的bin文件夹的路径作为环境变量.于是我 ...

  2. Identity Server 4 从入门到落地(六)—— 简单的单页面客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  3. Java8 Lambda表达式、函数式接口和方法引用

    目录 Java8 Lambda表达式和函数式接口 Lambda表达式 Lambda的使用 函数式接口FunctionalInterface Java内置四大核心函数式接口 方法引用 构造器引用 Jav ...

  4. nodejs-Cluster模块

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Cluster模块 GitHub TOP Cluster模块 来自<JavaScript 标准参考教程(alpha)&g ...

  5. CORS 如果需要指定多个域名怎么办

    CORS 通过控制 Access-Control-Allow-Origin 控制哪些域名可以共享资源,取值如下 Access-Control-Allow-Origin: <origin> ...

  6. Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null objec

    遇到这个一场折腾了1个小时, 这是系统在解析XML的时候出错, 最后费了好大的劲才发现 XML文件中,<View>  写成小写的 <view> 了. 崩溃啊.......... ...

  7. android studio 使用 aidl(三)权限验证

    这篇文章是基于android studio 使用 aidl (一) 和 android studio 使用 aidl(二) 异步回调 下面的代码都是简化的,如果看不懂请先移步上2篇文章 网上的东西太坑 ...

  8. linux修改文件权限命令

    先看个实例: [root@local opt]#ls -al ls -al 命令是列出目录的所有文件,包括隐藏文件.隐藏文件的文件名第一个字符为'.' -rw-r--r--  1 root root  ...

  9. SpringIOC原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  10. JDBC(2):JDBC对数据库进行CRUD

    一. statement对象 JDBC程序中的Connection用于代表数据库的链接:Statement对象用于向数据库发送SQL语句:ResultSet用于代表Sql语句的执行结果 JDBC中的s ...