Make delicious recipes!

Find maximum width of a tree


Solution 1: We can traverse the tree in level order fashion using a queue, inserting a dummy node when one level is completed. Then we know exactly when a level begins and when it ends and we can find maximum width.


Solution 2: Allocate an array of length tree-height to store node-counts at every level.

While traversing (in any traversal), increment the widths[level] at every node in the tree.

Then find the maximum in this array.




Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal