[LeetCode] 662. Maximum Width of Binary Tree

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.

二叉树的最大宽度。题意是给一个结构近似于满二叉树的二叉树,请你返回他的最大宽度是多少。

思路是BFS宽度优先搜索。做法是像level order traversal那样遍历树的每一个节点。在需要一个queue来存储遍历到的node的同时,需要另一个linked list来记录每个node的index。这里有一个结论需要记住,如果一个节点的index为i的话,他的左孩子的index是2 * i,他的右孩子的index是2 * i + 1。这个结论可以帮助规避中间有空节点的情况。

时间O(n)

空间O(n)

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         // corner case
13         if (root == null) {
14             return 0;
15         }
16 
17         // normal case
18         Queue<TreeNode> queue = new LinkedList<>();
19         LinkedList<Integer> list = new LinkedList<>();
20         int res = 1;
21         queue.offer(root);
22         list.add(1);
23         while (!queue.isEmpty()) {
24             int count = queue.size();
25             for (int i = 0; i < count; i++) {
26                 TreeNode cur = queue.poll();
27                 int curIndex = list.removeFirst();
28                 if (cur.left != null) {
29                     queue.offer(cur.left);
30                     list.offer(curIndex * 2);
31                 }
32                 if (cur.right != null) {
33                     queue.offer(cur.right);
34                     list.offer(curIndex * 2 + 1);
35                 }
36             }
37             if (list.size() >= 2) {
38                 res = Math.max(res, list.peekLast() - list.peekFirst() + 1);
39             }
40         }
41         return res;
42     }
43 }

 

LeetCode 题目总结

以上是关于[LeetCode] 662. Maximum Width of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-662.Maximum Width of Binary Tree

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

662. Maximum Width of Binary Tree

662二叉树最大宽度

每日一题662. 二叉树最大宽度

662. Maximum Width of Binary Tree二叉树的最大宽度