Maximum Width of a Binary Tree || <The most intuitive solution>

Catchy Thumbnail, haha
Before starting out, I promise you three things: you’ll have a better understanding of the concept of overflow, you’ll get an intuitive idea on Serialization of nodes in a Binary Tree, you’ll get accustomed to a hack.

Let’s now understand the question and come up with intuitions for the solution.

idea behind BFS traversal
width = last- first + 1
serialization of nodes
BFS traversal

The Hack

let’s take an example of following BT,

         A
/ \
B C
/ \ / \
D E F G

Great, now after marking is done, let’s simply traverse BFS and find the max width across all the levels.

final code
code accepted on Leetcode

--

--

I write articles on Android Development, and problem solving.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store