Given a Binary Tree and an integer **K**, the task is to find the level of the Binary Tree with width **K**. If multiple levels exists with width **K**, print the lowest level. If no such level exists, print **-1**.

The

widthof a level of a Binary tree is defined as the number of nodes betweenleftmostand therightmostnode at that level, including the NULL nodes in between them as well.

**Examples:**

Input:K = 45 --------- 1st level width = 1 => (5) / \ 6 2 -------- 2nd level width = 2 => (6, 2) / \ \ 7 3 8 -------3rd level width = 4 => (7, 3, NULL, 8) / \ 5 4 -----------4th level width = 4 => (5, NULL, NULL, 4)

Output:3

Explanation:

For the given tree, the levels having widthK( = 4) are 3 and 4.

Since 3 is the minimum of the two, print the minimum.

Input:K = 71 --------- 1st level width = 1 => (1) / \ 2 9 -------- 2nd level width = 2 => (2, 9) / \ 7 8 ---------3rd level width = 4 => (7, NULL, NULL, 8) / / 5 9 -----------4th level width = 7 => (5, NULL, NULL, / NULL, NULL, NULL, 9) 2 -----------5th level width = 1 => (2) / 1 -----------6th level width = 1 => (1)

Output:4

Explanation:

For the given tree, the level having widthK( = 7) is 4.

**Approach: **

The basic idea to solve the problem is to add a label to each node. If a parent has a label** i**, then assign a label **2*i** to it’s left child and **2*i+1** to its right child. This will help in including the NULL nodes in the calculation.

Follow the steps below:

- Perform
**Level Order Traversal**on the given tree using a**Queue**. - Queue contains a pair of {Node, Label}. Initially insert {
**rootNode, 0**} to queue. - If parent has label i, then for a left child, insert {
**leftChild, 2*i**} to queue and for right child, insert{**rightChild, 2*i+1**} into the queue. - For each level assume
**a**as label of leftmost node and**b**as label of rightmost node, then**(b-a+1)**gives the width of that level. - Check whether the width is equal to
**K**. If so, return**level**. - If none of the levels have width
**K**, then return**-1**.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure of a Tree node ` `struct` `Node { ` ` ` `int` `key; ` ` ` `struct` `Node *left, *right; ` `}; ` ` ` `// Utility function to create ` `// and initialize a new node ` `Node* newNode(` `int` `key) ` `{ ` ` ` `Node* temp = ` `new` `Node; ` ` ` `temp->key = key; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `(temp); ` `} ` ` ` `// Function returns required level ` `// of width k, if found else -1 ` `int` `findLevel(Node* root, ` ` ` `int` `k, ` `int` `level) ` `{ ` ` ` ` ` `// To store the node and the label ` ` ` `// and perform traversal ` ` ` `queue<pair<Node*, ` `int` `> > qt; ` ` ` `qt.push(make_pair(root, 0)); ` ` ` ` ` `int` `count = 1, b, a = 0; ` ` ` ` ` `while` `(!qt.empty()) { ` ` ` ` ` `pair<Node*, ` `int` `> temp = qt.front(); ` ` ` `qt.pop(); ` ` ` ` ` `// Taking the last label ` ` ` `// of each level of the tree ` ` ` `if` `(count == 1) { ` ` ` `b = temp.second; ` ` ` `} ` ` ` ` ` `if` `((temp.first)->left) { ` ` ` `qt.push(make_pair( ` ` ` `temp.first->left, ` ` ` `2 * temp.second)); ` ` ` `} ` ` ` `if` `(temp.first->right) { ` ` ` `qt.push(make_pair( ` ` ` `temp.first->right, ` ` ` `2 * temp.second + 1)); ` ` ` `} ` ` ` ` ` `count--; ` ` ` ` ` `// Check width of current level ` ` ` `if` `(count == 0) { ` ` ` ` ` `// If the width is equal to k ` ` ` `// then return that level ` ` ` `if` `(b - a + 1 == k) ` ` ` `return` `level; ` ` ` ` ` `pair<Node*, ` `int` `> secondLabel = qt.front(); ` ` ` ` ` `// Taking the first label ` ` ` `// of each level of the tree ` ` ` `a = secondLabel.second; ` ` ` ` ` `level += 1; ` ` ` `count = qt.size(); ` ` ` `} ` ` ` `} ` ` ` ` ` `// If any level does not has ` ` ` `// width equal to k, return -1 ` ` ` `return` `-1; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `Node* root = newNode(5); ` ` ` `root->left = newNode(6); ` ` ` `root->right = newNode(2); ` ` ` `root->right->right = newNode(8); ` ` ` ` ` `root->left->left = newNode(7); ` ` ` `root->left->left->left = newNode(5); ` ` ` `root->left->right = newNode(3); ` ` ` `root->left->right->right = newNode(4); ` ` ` ` ` `int` `k = 4; ` ` ` ` ` `cout << findLevel(root, k, 1) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**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.

## Recommended Posts:

- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Find maximum level sum in Binary Tree
- Find if given vertical level of binary tree is sorted or not
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find Maximum Level Sum in Binary Tree using Recursion
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Maximum width of a binary tree
- Vertical width of Binary tree | Set 1
- Vertical width of Binary tree | Set 2
- Connect Nodes at same Level (Level Order Traversal)
- Get Level of a node in a Binary Tree
- Print nodes between two given level numbers of a binary tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.