


For the given binary tree

The maximum width will be at the third level with the length of 3, i.e. {4, 5, 6}.
The only line of input contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node(of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null(-1).
The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Print a single line that contains a single integer that denotes the maximum width for the given tree.
You do not need to print anything; it has already been taken care of. Just implement the given function.
The straightforward intuition is that first, find the maximum levels possible in the given tree, called the "HEIGHT" of the tree. And then, for each level in height, we will find the number of nodes in each level. And the maximum number of the node among all levels will be the maximum width of the given binary tree. So the implementation of our intuition takes bellow steps:
2. We will go for each level in [1: "HEIGHT"] and find how many nodes it contains at that level. The maximum number of nodes among all levels will be the maximum width of the given tree. For finding the number of nodes at any level, let's say "LEVEL", Steps are as follows:
3. The maximum number of nodes among all levels will be the maximum width of the given tree.
In Approach 1, to get the total number of nodes on each level, we used a recursive algorithm. That took cost O(N*H), where 'N' is the number of nodes in the given binary tree and 'H' is the height of the binary tree. But in this approach, our intuition is that as we know about preorder, postorder, or inorder traversal visits each element only once. We will be using any one of these three, and while calling the function, we will also pass the level corresponding to that node. And when we visit any node, we will increment the total number of nodes corresponding to the level of that node. The steps are as follows :
In this approach, we are going to use the level order traversal algorithm. While traversing in level order, we will be storing all the children of the nodes at the current level in the queue. And once we store all the children nodes for the current level, our queue will have all the next-level nodes. So, We can get the total number of nodes for the next level from the size of the queue. And we keep exploring and maintaining the maximum number of nodes in a level (i.e. the maximum size of the queue at any level) that we got till now. In the end, we have a maximum number of nodes that could be at any level. The steps are as follows:
Inorder Traversal
Inorder Traversal
Inorder Traversal
Inorder Traversal
Inorder Traversal
Postorder Traversal
Postorder Traversal
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Locked Binary Tree
Maximum Island Size in a Binary Tree