java.lang.Object
g0601_0700.s0662_maximum_width_of_binary_tree.Solution

public class Solution extends Object
662 - Maximum Width of Binary Tree.<p>Medium</p> <p>Given the <code>root</code> of a binary tree, return <em>the <strong>maximum width</strong> of the given tree</em>.</p> <p>The <strong>maximum width</strong> of a tree is the maximum <strong>width</strong> among all levels.</p> <p>The <strong>width</strong> of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end-nodes are also counted into the length calculation.</p> <p>It is <strong>guaranteed</strong> that the answer will in the range of <strong>32-bit</strong> signed integer.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,3,2,5,3,null,9]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The maximum width existing in the third level with the length 4 (5,3,null,9).</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/05/03/width2-tree.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,3,null,5,3]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The maximum width existing in the third level with the length 2 (5,3).</p> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,3,2,5]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> The maximum width existing in the second level with the length 2 (3,2).</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 3000]</code>.</li> <li><code>-100 <= Node.val <= 100</code></li> </ul>
  • Constructor Details Link icon

    • Solution Link icon

      public Solution()
  • Method Details Link icon

    • widthOfBinaryTree Link icon

      public int widthOfBinaryTree(TreeNode root)