[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

从第三层开始,如果已经满了,就要添加新的index

if (level == list.size()) list.add(index);

[思维问题]:

完全没思路,因此需要一些基础知识

[英文数据结构或算法,为什么不用别的数据结构或算法]:

We know that a binary tree can be represented by an array (assume the root begins from the position with index 1 in the array). If the index of a node is i, the indices of its two children are 2*i and 2*i + 1. The idea is to use two arrays (start[] and end[]) to record the the indices of the leftmost node and rightmost node in each level, respectively. For each level of the tree, the width isend[level] - start[level] + 1. Then, we just need to find the maximum width.

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

index的初始值为啥是1?不懂,算了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

找参数只是结果,重要的是把所需的变量找出来

还是按照起点、过程、终点来写,index的左右分别为 2 * index 和  2 * index + 1,

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 1; public int widthOfBinaryTree(TreeNode root) {
//corner case
if (root == null) return 0; //initialization
List<Integer> startOfLevel = new ArrayList<Integer>(); //return
getWidth(root, 1, 0, startOfLevel);
return max;
} public void getWidth(TreeNode root, int index, int level, List<Integer> list) {
//return null
if (root == null) return ; //add the index to list
if (list.size() == level)
list.add(index); max = Math.max(max, index + 1 - list.get(level)); //divide and conquer in left and right
getWidth(root.left, 2 * index, level + 1, list);
getWidth(root.right, 2 * index + 1, level + 1, list);
}
}

662. Maximum Width of Binary Tree二叉树的最大宽度的更多相关文章

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

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

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

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

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

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

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

  7. 662. Maximum Width of Binary Tree

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

  8. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

随机推荐

  1. 在执行bat脚本的时候打印日志

  2. 链表有环判断,快慢指针两种方法/合并链表/删除重复元素/二分递归和while

    public static boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return fals ...

  3. 一个Tparams小测试

    var aParams: TParams; aPar: TParam; I:Integer; begin aParams := TParams.Create(nil); aPar := aParams ...

  4. Android Studio3.1.2编译时Java Compiler出错:Warning: Failed to parse host proxy3.bj...

    删除gradle.properties中的代理设置... #移除下面配置systemProp.http.proxyHost=proxy3.bj.petrochina systemProp.http.p ...

  5. mysql分表实战

    本文主要讲述如何使用存储过程完成本表.并不讨论其他问题.首先我们得看看手册上关于meger引擎的说明: MERGE存储引擎,也被认识为MRG_MyISAM引擎,是一个相同的可以被当作一个来用的MyIS ...

  6. SQL--结构化的查询语言

    SQL--结构化的查询语言T-SQL:Transact-SQL (SQL的增强版) 逻辑运算符 and && or || not ! 关系运算符 等于 = 不等于<>或!= ...

  7. Oracle ORA-00911: 无效字符

    SQL语句后多了个分号 “ ; ”.

  8. Genomic signatures of evolutionary transitions from solitary to group living(独居到社会性的转变)

    1.摘要 群居性的进化是进化的主要过渡之一,但其背后的基因组变化是未知的.我们比较了10种蜜蜂的基因组,它们的社会复杂性各不相同,代表了社会进化中的多种独立过渡,并报告了三项主要发现. 第一,许多重要 ...

  9. 什么是webFlux

    什么是webFlux 左侧是传统的基于Servlet的Spring Web MVC框架,右侧是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,从上到下依次是R ...

  10. mysql创建用户和库

    先用root登陆mysql,然后运行下面代码创建mysql用户和库 下面创建的帐号:test123  密码:123456  库名:test123 CREATE USER '; GRANT USAGE ...