# Print all K-sum levels in a Binary Tree

Given a Binary Tree and an integer K where the tree has positive and negative nodes, the task is to print the elements of the level whose sum equals K. If no such result exists, then print “Not Possible“.

Examples:

```Input:
-10
/    \
2      -3
/   \       \
4     15      -6
/       \      /
7         -8   9
K = 13
Output: 4 15 -6
Explanation:
Level 1 (-10): Sum = -10
Level 2 (2, 3): Sum = 5
Level 3 (4, 15, -6): Sum = 13
Level 4 (7, -8, 9): Sum = 8
Only level 3 (4, 15, -6) has sum = K

Input:
1
/  \
12    13
/     /   \
11    6    -11
\    /
2   2
K = 30
Output:  Not Possible
Explanation:
There is no such level whose sum = K
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Perform level order traversal of the Binary tree and store find the sum of each level.
• If the sum is equal to K, prin the level. Else move to the next level.
• The process is repeated till all the levels has been traversed and checked.
• If there is no such level with sum K, print “Not Possible”.

Below is the implementation of the above approach:

## C++

 `// C++ program to print all ` `// K-sum levels in a Binary Tree ` `#include ` `using` `namespace` `std; ` ` `  `// Vector to store the ` `// elements of a level ` `vector<``int``> level; ` ` `  `// Binary Tree Node ` `struct` `node { ` `    ``struct` `node* left; ` `    ``int` `data; ` `    ``struct` `node* right; ` `}; ` ` `  `// Function to display elements ` `void` `display(``bool` `flag) ` `{ ` ` `  `    ``// Check if boolean variable is true ` `    ``// then print the level ` `    ``if` `(flag) { ` ` `  `        ``for` `(``auto` `x : level) ` `            ``cout << x << ``" "``; ` `    ``} ` ` `  `    ``else` ` `  `        ``cout << ``"Not Possible\n"``; ` `} ` ` `  `// Function to find sum of ` `// elements by level order traversal ` `void` `SumlevelOrder(node* root, ``int` `k) ` `{ ` ` `  `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``// Queue data structure for ` `    ``// level order tarversal ` `    ``queue q; ` ` `  `    ``// Enqueue Root in Queue ` `    ``q.push(root); ` ` `  `    ``bool` `flag = ``false``; ` ` `  `    ``while` `(q.empty() == ``false``) { ` ` `  `        ``// number of nodes at current level ` `        ``int` `nodeCount = q.size(); ` ` `  `        ``int` `Present_level_sum = 0; ` ` `  `        ``// Dequeue all nodes of current level and ` `        ``// Enqueue all nodes of next level ` `        ``while` `(nodeCount > 0) { ` ` `  `            ``node* node = q.front(); ` ` `  `            ``// To add node data ` `            ``Present_level_sum += node->data; ` ` `  `            ``level.push_back(node->data); ` ` `  `            ``q.pop(); ` ` `  `            ``if` `(node->left != NULL) ` `                ``q.push(node->left); ` ` `  `            ``if` `(node->right != NULL) ` `                ``q.push(node->right); ` ` `  `            ``nodeCount--; ` `        ``} ` ` `  `        ``if` `(Present_level_sum == k) { ` ` `  `            ``flag = ``true``; ` `            ``break``; ` `        ``} ` ` `  `        ``level.clear(); ` `    ``} ` ` `  `    ``display(flag); ` `} ` ` `  `// Function to create a new tree node ` `node* newNode(``int` `data) ` `{ ` `    ``node* temp = ``new` `node; ` `    ``temp->data = data; ` `    ``temp->left = NULL; ` `    ``temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Create binary tree ` `    ``node* root = newNode(1); ` ` `  `    ``root->left = newNode(2); ` `    ``root->right = newNode(3); ` ` `  `    ``root->left->left = newNode(4); ` `    ``root->left->right = newNode(5); ` `    ``root->right->right = newNode(6); ` ` `  `    ``int` `K = 15; ` ` `  `    ``SumlevelOrder(root, K); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to print all ` `// K-sum levels in a Binary Tree ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Vector to store the ` `// elements of a level ` `static` `Vector level = ``new` `Vector(); ` `  `  `// Binary Tree Node ` `static` `class` `node { ` `    ``node left; ` `    ``int` `data; ` `    ``node right; ` `}; ` `  `  `// Function to display elements ` `static` `void` `display(``boolean` `flag) ` `{ ` `  `  `    ``// Check if boolean variable is true ` `    ``// then print the level ` `    ``if` `(flag) { ` `  `  `        ``for` `(Integer x : level) ` `            ``System.out.print(x+ ``" "``); ` `    ``} ` `  `  `    ``else` `  `  `        ``System.out.print(``"Not Possible\n"``); ` `} ` `  `  `// Function to find sum of ` `// elements by level order traversal ` `static` `void` `SumlevelOrder(node root, ``int` `k) ` `{ ` `  `  `    ``if` `(root == ``null``) ` `        ``return``; ` `  `  `    ``// Queue data structure for ` `    ``// level order tarversal ` `    ``Queue q = ``new` `LinkedList<>(); ` `  `  `    ``// Enqueue Root in Queue ` `    ``q.add(root); ` `  `  `    ``boolean` `flag = ``false``; ` `  `  `    ``while` `(q.isEmpty() == ``false``) { ` `  `  `        ``// number of nodes at current level ` `        ``int` `nodeCount = q.size(); ` `  `  `        ``int` `Present_level_sum = ``0``; ` `  `  `        ``// Dequeue all nodes of current level and ` `        ``// Enqueue all nodes of next level ` `        ``while` `(nodeCount > ``0``) { ` `  `  `            ``node node = q.peek(); ` `  `  `            ``// To add node data ` `            ``Present_level_sum += node.data; ` `  `  `            ``level.add(node.data); ` `  `  `            ``q.remove(); ` `  `  `            ``if` `(node.left != ``null``) ` `                ``q.add(node.left); ` `  `  `            ``if` `(node.right != ``null``) ` `                ``q.add(node.right); ` `  `  `            ``nodeCount--; ` `        ``} ` `  `  `        ``if` `(Present_level_sum == k) { ` `  `  `            ``flag = ``true``; ` `            ``break``; ` `        ``} ` `  `  `        ``level.clear(); ` `    ``} ` `  `  `    ``display(flag); ` `} ` `  `  `// Function to create a new tree node ` `static` `node newNode(``int` `data) ` `{ ` `    ``node temp = ``new` `node(); ` `    ``temp.data = data; ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``; ` `    ``return` `temp; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``// Create binary tree ` `    ``node root = newNode(``1``); ` `  `  `    ``root.left = newNode(``2``); ` `    ``root.right = newNode(``3``); ` `  `  `    ``root.left.left = newNode(``4``); ` `    ``root.left.right = newNode(``5``); ` `    ``root.right.right = newNode(``6``); ` `  `  `    ``int` `K = ``15``; ` `  `  `    ``SumlevelOrder(root, K); ` `  `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## C#

 `// C# program to print all ` `// K-sum levels in a Binary Tree ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` `   `  `// List to store the ` `// elements of a level ` `static` `List<``int``> level = ``new` `List<``int``>(); ` `   `  `// Binary Tree Node ` `class` `node { ` `    ``public` `node left; ` `    ``public` `int` `data; ` `    ``public` `node right; ` `}; ` `   `  `// Function to display elements ` `static` `void` `display(``bool` `flag) ` `{ ` `   `  `    ``// Check if bool variable is true ` `    ``// then print the level ` `    ``if` `(flag) { ` `   `  `        ``foreach` `(``int` `x ``in` `level) ` `            ``Console.Write(x+ ``" "``); ` `    ``} ` `   `  `    ``else` `   `  `        ``Console.Write(``"Not Possible\n"``); ` `} ` `   `  `// Function to find sum of ` `// elements by level order traversal ` `static` `void` `SumlevelOrder(node root, ``int` `k) ` `{ ` `   `  `    ``if` `(root == ``null``) ` `        ``return``; ` `   `  `    ``// Queue data structure for ` `    ``// level order tarversal ` `    ``Queue q = ``new` `Queue(); ` `   `  `    ``// Enqueue Root in Queue ` `    ``q.Enqueue(root); ` `   `  `    ``bool` `flag = ``false``; ` `   `  `    ``while` `(q.Count!=0) { ` `   `  `        ``// number of nodes at current level ` `        ``int` `nodeCount = q.Count; ` `   `  `        ``int` `Present_level_sum = 0; ` `   `  `        ``// Dequeue all nodes of current level and ` `        ``// Enqueue all nodes of next level ` `        ``while` `(nodeCount > 0) { ` `   `  `            ``node node = q.Peek(); ` `   `  `            ``// To add node data ` `            ``Present_level_sum += node.data; ` `   `  `            ``level.Add(node.data); ` `   `  `            ``q.Dequeue(); ` `   `  `            ``if` `(node.left != ``null``) ` `                ``q.Enqueue(node.left); ` `   `  `            ``if` `(node.right != ``null``) ` `                ``q.Enqueue(node.right); ` `   `  `            ``nodeCount--; ` `        ``} ` `   `  `        ``if` `(Present_level_sum == k) { ` `   `  `            ``flag = ``true``; ` `            ``break``; ` `        ``} ` `   `  `        ``level.Clear(); ` `    ``} ` `   `  `    ``display(flag); ` `} ` `   `  `// Function to create a new tree node ` `static` `node newNode(``int` `data) ` `{ ` `    ``node temp = ``new` `node(); ` `    ``temp.data = data; ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``; ` `    ``return` `temp; ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``// Create binary tree ` `    ``node root = newNode(1); ` `   `  `    ``root.left = newNode(2); ` `    ``root.right = newNode(3); ` `   `  `    ``root.left.left = newNode(4); ` `    ``root.left.right = newNode(5); ` `    ``root.right.right = newNode(6); ` `   `  `    ``int` `K = 15; ` `   `  `    ``SumlevelOrder(root, K); ` `   `  `} ` `} ` `  `  `// This code is contributed by sapnasingh4991 `

Output:

```4 5 6
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.