21 · Tree Traversals

#662 Maximum Width of Binary Tree

Mediumsolve on LeetCode ↗

frequently reported atAmazonBloombergGoogleMicrosoftMeta

the problem

In plain words

The width of a level is the number of positions from its leftmost node to its rightmost node, counting the empty spots in between as if the tree were complete there. Return the largest width of any level. The answer fits in a 32-bit signed int.

in root = [1,3,2,5,3,null,9]

out 4

Level 2 is 5, 3, (empty), 9 — four positions.

in root = [1,3,2,5,null,null,9,6,null,7]

out 7

Level 3 is 6, five empty spots, then 7.

in root = [1,3,2,5]

out 2

  • 1 ≤ number of nodes ≤ 3000
  • -100 ≤ Node.val ≤ 100

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #102
    2. #199
      Binary Tree Right Side View
      Mediumin Traversals