原题链接在这里:https://leetcode.com/problems/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.

题解:

把每个点笔记上id. cur.id = i, 那么cur.left.id = 2*i, cur.right.id = 2*i+1.

找到每一行开始点的id, start. 和最后点的id, end. 这一行的最大宽度就是end-start+1.

Time Complexity: O(n).

Space: O(n).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int widthOfBinaryTree(TreeNode root) {
12         if(root == null){
13             return 0;
14         }
15
16         LinkedList<Pair> que = new LinkedList<Pair>();
17         que.add(new Pair(root,1));
18         int curCount = 1;
19         int nextCount = 0;
20
21         int res = 1;
22         int start = 1;
23         int end = start;
24
25         while(!que.isEmpty()){
26             Pair cur = que.poll();
27             curCount--;
28             end = cur.id;
29
30             if(cur.node.left != null){
31                 que.add(new Pair(cur.node.left, cur.id*2));
32                 nextCount++;
33             }
34
35             if(cur.node.right != null){
36                 que.add(new Pair(cur.node.right, cur.id*2+1));
37                 nextCount++;
38             }
39
40             if(curCount == 0){
41                 res = Math.max(res, end-start+1);
42                 curCount = nextCount;
43                 nextCount = 0;
44                 start = que.isEmpty() ? 1 : que.peek().id;
45                 end = start;
46             }
47         }
48
49         return res;
50     }
51 }
52
53 class Pair{
54     TreeNode node;
55     int id;
56     public Pair(TreeNode node, int id){
57         this.node = node;
58         this.id = id;
59     }
60 }

类似Binary Tree Level Order Traversal.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/10982764.html

LeetCode 662. Maximum Width of Binary Tree相关推荐

  1. leetcode 662. Maximum Width of Binary Tree | 662. 二叉树最大宽度(BFS)

    题目 https://leetcode.com/problems/maximum-width-of-binary-tree/ 题解 本题思路来源于二叉树的层序遍历. 层序遍历类似问题:leetcode ...

  2. Leetcode | Minimum/Maximum Depth of Binary Tree

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

  3. 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 l ...

  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之Maximum Depth of Binary Tree

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

  6. [swift] 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 ...

  7. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  8. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  9. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

最新文章

  1. 简述python中怎样导入模块_Python中导入模块的两种模式,import
  2. CentOS 安装配置memcached
  3. [Linux] Vmware 15安装CentOs后显示网络不可用
  4. 怎么查找那台电脑中了ARP病毒
  5. Quartz.Net分布式任务管理平台
  6. 利用链表实现可合并堆(算法导论第三版思考题10-2)
  7. Lucene组件概述
  8. HMI使用自定义控件流程
  9. new出来的对象怎么回收_JVM的内存模型及垃圾回收算法
  10. 信息提示无法建立数据连接服务器,FileZilla 链接FTP服务器无法建立数据连接: ECONNREFUSED...
  11. macOS Unlocker3.0
  12. mysql高效索引覆盖索引_MySQL高效索引:覆盖索引
  13. D3 Handling Events
  14. 能搞垮你的不止是同行
  15. SpringMVC Ⅰ
  16. Nexus下载构件失败
  17. 邮箱服务器 拦截策略,企业邮箱服务器的安全管理策略
  18. 食品级L-天门冬氨酸市场现状及未来发展趋势
  19. 互联网晚报 | 12月25日 星期六 | 小米首款自研充电芯片澎湃P1官宣;抖音电商启动“冬季山货节”;全国首批“千兆城市”出炉...
  20. Python(arcpy) 批量shp转raster

热门文章

  1. 使用hierarchyid查询分层数据
  2. nginx 伪静态php去掉后缀_Nginx与PHP是如何进行交互的?
  3. mysql查看连接数命令_Mysql 查看连接数,状态
  4. 家庭用计算机怎样选择设置网络位置,win7系统怎么选择网络位置
  5. 怎么检测不到我的音频_新专利显示未来的AirPods可能会检测手势 并具有旋转式音量控制功能...
  6. arch linux rpm格式,如何在ArchLinux上安装RPM包
  7. c防止随机数重复_铝及铝模板等焊接常见缺陷、和防止措施12招
  8. python字符串可以使用+进行计算吗_python用正则对字符串进行运算
  9. python内置数学函数库_在没有任何内置的求值函数或外部库的情况下用python解决数学问题...
  10. java求梯形面积程序_Java初级应用,计算关于梯形跟圆形的面积。该程序中有3个类:Lader、Circle和主类Test。...