Given an N-ary tree, the task is to find the maximum width of the given tree. The maximum width of a tree is the maximum of width among all levels.
Input:4 / | \ 2 3 -5 / \ /\ -1 3 -2 6
Width of 0th level is 1.
Width of 1st level is 3.
Width of 2nd level is 4.
Therefore, the maximum width is 4
1 / | \ 2 -1 3 / \ \ 4 5 8 / / | \ 2 6 12 7
Approach: This problem can be solved using BFS. The idea is to perform level order traversal of the tree. While doing traversal, process nodes of different levels separately. For every level being processed, count the number of nodes present at each level and keep track of the maximum count. Follow the steps below to solve the problem:
- Initialize a variable, say maxWidth to store the required maximum width of the tree.
- Initialize a Queue to perform the level order traversal of the given tree.
- Push the root node into the queue.
- If size of the queue exceeds maxWidth for any level, then update maxWidth to the size of the queue.
- Traverse the queue and push all the nodes of the next level into the queue and pop all the nodes of the current level.
- Repeat the above steps until all the levels of the tree are traversed.
- Finally, return the final value of maxWidth.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum width of a binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Vertical width of Binary tree | Set 1
- Vertical width of Binary tree | Set 2
- Find the Level of a Binary Tree with Width K
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree into its Mirror Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a binary tree is subtree of another binary tree | Set 1
- Convert a given tree to its Sum Tree
- Binary Tree to Binary Search Tree Conversion
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search tree
- Binary Tree | Set 3 (Types of Binary Tree)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29