# Sum of all nodes at given level in Binary Tree

Here given code implementation process.

``````/*
C Program
Sum of all nodes at given level in Binary Tree
*/
#include <stdio.h>
#include <stdlib.h>

//Binary Tree node
struct Node
{
int data;
struct Node *left, *right;
};

//This is creating a binary tree node and return this
struct Node *get_node(int data)
{
// Create dynamic node
struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
if (new_node != NULL)
{
//Set data and pointer values
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
}
else
{
//This is indicates, segmentation fault or memory overflow problem
printf("Memory Overflow\n");
}
//return new node
return new_node;
}
//Display pre order elements
void preorder(struct Node *node)
{
if (node != NULL)
{
//Print node value
printf("  %d", node->data);
preorder(node->left);
preorder(node->right);
}
}
// Find the sum of the nodes of at given level of the binary tree
int sum_level(struct Node *node, int level, int k)
{
if (node == NULL)
{
return 0;
}
if (k == level)
{
return node->data;
}
return sum_level(node->left, level, k + 1) + sum_level(node->right, level, k + 1);
}
// Handles the request to find sum of nodes at given level
void sum_of_level(struct Node *root, int level)
{
if (level <= 0)
{
//Invalid level
return;
}
else if (root == NULL)
{
printf("\n Empty Tree\n");
}
else
{
int result = sum_level(root, level, 1);
// Display calculated result
printf(" Level %d node sum is : %d\n", level, result);
}
}
int main()
{
struct Node *root = NULL;
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4

-----------------
*/
root = get_node(20);
root->left = get_node(5);
root->left->right = get_node(3);
root->left->right->left = get_node(10);
root->left->right->right = get_node(8);
root->left->right->right->right = get_node(4);
root->left->left = get_node(1);
root->right = get_node(7);
root->right->right = get_node(2);
root->right->right->right = get_node(9);
printf("\n Tree Nodes \n");
preorder(root);
printf("\n");
//Test Case
sum_of_level(root, 4);
sum_of_level(root, 1);
sum_of_level(root, 3);
sum_of_level(root, 5);
sum_of_level(root, 2);
return 0;
}``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````/*
Java Program
Sum of all nodes at given level in Binary Tree
*/

// Binary Tree node
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
// Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
//Set initial tree root to null
this.root = null;
}
//Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
//Print node value
System.out.print("  " + node.data);
preorder(node.left);
preorder(node.right);
}
}
// Find the sum of the nodes of at given level of the binary tree
public int sum_level(Node node, int level, int k)
{
if (node == null)
{
return 0;
}
if (k == level)
{
return node.data;
}
return sum_level(node.left, level, k + 1) + sum_level(node.right, level, k + 1);
}
// Handles the request to find sum of nodes at given level
public void sum_of_level(int level)
{
if (level <= 0)
{
//Invalid level
return;
}
else if (this.root == null)
{
System.out.print("\n Empty Tree\n");
}
else
{
int result = sum_level(this.root, level, 1);
// Display calculated result
System.out.print(" Level " + level + " node sum is : " + result + "\n");
}
}
public static void main(String[] args)
{
//Create tree object
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4

-----------------
*/
tree.root = new Node(20);
tree.root.left = new Node(5);
tree.root.left.right = new Node(3);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(8);
tree.root.left.right.right.right = new Node(4);
tree.root.left.left = new Node(1);
tree.root.right = new Node(7);
tree.root.right.right = new Node(2);
tree.root.right.right.right = new Node(9);
System.out.print("\n Tree Nodes \n");
tree.preorder(tree.root);
System.out.print("\n");
//Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
}
}``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program
Sum of all nodes at given level in Binary Tree
*/
//  Binary Tree node
class Node
{
public: int data;
Node *left;
Node *right;
Node(int data)
{
//  Set node value
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
class BinaryTree
{
public: Node *root;
BinaryTree()
{
// Set initial tree root to null
this->root = NULL;
}
// Display pre order elements
void preorder(Node *node)
{
if (node != NULL)
{
// Print node value
cout << "  " << node->data;
this->preorder(node->left);
this->preorder(node->right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
int sum_level(Node *node, int level, int k)
{
if (node == NULL)
{
return 0;
}
if (k == level)
{
return node->data;
}
return this->sum_level(node->left, level, k + 1) + this->sum_level(node->right, level, k + 1);
}
//  Handles the request to find sum of nodes at given level
void sum_of_level(int level)
{
// Invalid level
if (level <= 0)
{
return;
}
else if (this->root == NULL)
{
cout << "\n Empty Tree\n";
}
else
{
int result = this->sum_level(this->root, level, 1);
//  Display calculated result
cout << " Level " << level << " node sum is : " << result << "\n";
}
}
};
int main()
{
// Create tree object
BinaryTree tree = BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
tree.root = new Node(20);
tree.root->left = new Node(5);
tree.root->left->right = new Node(3);
tree.root->left->right->left = new Node(10);
tree.root->left->right->right = new Node(8);
tree.root->left->right->right->right = new Node(4);
tree.root->left->left = new Node(1);
tree.root->right = new Node(7);
tree.root->right->right = new Node(2);
tree.root->right->right->right = new Node(9);
cout << "\n Tree Nodes \n";
tree.preorder(tree.root);
cout << "\n";
// Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
return 0;
}``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````// Include namespace system
using System;

/*
C# Program
Sum of all nodes at given level in Binary Tree
*/

//  Binary Tree node
public class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
//  Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTree
{
public Node root;
public BinaryTree()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
public void preorder(Node node)
{
if (node != null)
{
// Print node value
Console.Write("  " + node.data);
preorder(node.left);
preorder(node.right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
public int sum_level(Node node, int level, int k)
{
if (node == null)
{
return 0;
}
if (k == level)
{
return node.data;
}
return sum_level(node.left, level, k + 1) + sum_level(node.right, level, k + 1);
}
//  Handles the request to find sum of nodes at given level
public void sum_of_level(int level)
{
// Invalid level
if (level <= 0)
{
return;
}
else if (this.root == null)
{
Console.Write("\n Empty Tree\n");
}
else
{
int result = sum_level(this.root, level, 1);
//  Display calculated result
Console.Write(" Level " + level + " node sum is : " + result + "\n");
}
}
public static void Main(String[] args)
{
// Create tree object
BinaryTree tree = new BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
tree.root = new Node(20);
tree.root.left = new Node(5);
tree.root.left.right = new Node(3);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(8);
tree.root.left.right.right.right = new Node(4);
tree.root.left.left = new Node(1);
tree.root.right = new Node(7);
tree.root.right.right = new Node(2);
tree.root.right.right.right = new Node(9);
Console.Write("\n Tree Nodes \n");
tree.preorder(tree.root);
Console.Write("\n");
// Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
}
}``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````<?php
/*
Php Program
Sum of all nodes at given level in Binary Tree
*/

//  Binary Tree node
class Node
{
public \$data;
public \$left;
public \$right;

function __construct(\$data)
{
//  Set node value
\$this->data = \$data;
\$this->left = null;
\$this->right = null;
}
}
class BinaryTree
{
public \$root;

function __construct()
{
// Set initial tree root to null
\$this->root = null;
}
// Display pre order elements
public	function preorder(\$node)
{
if (\$node != null)
{
// Print node value
echo "  ". \$node->data;
\$this->preorder(\$node->left);
\$this->preorder(\$node->right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
public	function sum_level(\$node, \$level, \$k)
{
if (\$node == null)
{
return 0;
}
if (\$k == \$level)
{
return \$node->data;
}
return \$this->sum_level(\$node->left, \$level, \$k + 1) + \$this->sum_level(\$node->right, \$level, \$k + 1);
}
//  Handles the request to find sum of nodes at given level
public	function sum_of_level(\$level)
{
// Invalid level
if (\$level <= 0)
{
return;
}
else if (\$this->root == null)
{
echo "\n Empty Tree\n";
}
else
{
\$result = \$this->sum_level(\$this->root, \$level, 1);
//  Display calculated result
echo " Level ". \$level ." node sum is : ". \$result ."\n";
}
}
}

function main()
{
// Create tree object
\$tree = new BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
\$tree->root = new Node(20);
\$tree->root->left = new Node(5);
\$tree->root->left->right = new Node(3);
\$tree->root->left->right->left = new Node(10);
\$tree->root->left->right->right = new Node(8);
\$tree->root->left->right->right->right = new Node(4);
\$tree->root->left->left = new Node(1);
\$tree->root->right = new Node(7);
\$tree->root->right->right = new Node(2);
\$tree->root->right->right->right = new Node(9);
echo "\n Tree Nodes \n";
\$tree->preorder(\$tree->root);
echo "\n";
// Test Case
\$tree->sum_of_level(4);
\$tree->sum_of_level(1);
\$tree->sum_of_level(3);
\$tree->sum_of_level(5);
\$tree->sum_of_level(2);
}
main();``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````/*
Node Js Program
Sum of all nodes at given level in Binary Tree
*/

//  Binary Tree node
class Node
{
constructor(data)
{
//  Set node value
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree
{
constructor()
{
// Set initial tree root to null
this.root = null;
}
// Display pre order elements
preorder(node)
{
if (node != null)
{
// Print node value
process.stdout.write("  " + node.data);
this.preorder(node.left);
this.preorder(node.right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
sum_level(node, level, k)
{
if (node == null)
{
return 0;
}
if (k == level)
{
return node.data;
}
return this.sum_level(node.left, level, k + 1) + this.sum_level(node.right, level, k + 1);
}
//  Handles the request to find sum of nodes at given level
sum_of_level(level)
{
// Invalid level
if (level <= 0)
{
return;
}
else if (this.root == null)
{
process.stdout.write("\n Empty Tree\n");
}
else
{
var result = this.sum_level(this.root, level, 1);
//  Display calculated result
process.stdout.write(" Level " + level + " node sum is : " + result + "\n");
}
}
}

function main()
{
// Create tree object
var tree = new BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
tree.root = new Node(20);
tree.root.left = new Node(5);
tree.root.left.right = new Node(3);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(8);
tree.root.left.right.right.right = new Node(4);
tree.root.left.left = new Node(1);
tree.root.right = new Node(7);
tree.root.right.right = new Node(2);
tree.root.right.right.right = new Node(9);
process.stdout.write("\n Tree Nodes \n");
tree.preorder(tree.root);
process.stdout.write("\n");
// Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
}
main();``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````#     Python 3 Program
#     Sum of all nodes at given level in Binary Tree

#  Binary Tree node
class Node :

def __init__(self, data) :
#  Set node value
self.data = data
self.left = None
self.right = None

class BinaryTree :

def __init__(self) :
# Set initial tree root to null
self.root = None

# Display pre order elements
def preorder(self, node) :
if (node != None) :
# Print node value
print("  ", node.data, end = "")
self.preorder(node.left)
self.preorder(node.right)

#  Find the sum of the nodes of at given level of the binary tree
def sum_level(self, node, level, k) :
if (node == None) :
return 0

if (k == level) :
return node.data

return self.sum_level(node.left, level, k + 1) + self.sum_level(node.right, level, k + 1)

#  Handles the request to find sum of nodes at given level
def sum_of_level(self, level) :
if (level <= 0) :
# Invalid level
return

elif(self.root == None) :
print("\n Empty Tree\n", end = "")
else :
result = self.sum_level(self.root, level, 1)
#  Display calculated result
print(" Level ", level ," node sum is : ", result ,"\n", end = "")

def main() :
# Create tree object
tree = BinaryTree()
#
#         constructor binary tree
#         -----------------
#             20
#            /   \
#           5     7
#          / \     \
#         1   3     2
#            / \     \
#           10  8     9
#                \
#                 4
#         -----------------
#

tree.root = Node(20)
tree.root.left = Node(5)
tree.root.left.right = Node(3)
tree.root.left.right.left = Node(10)
tree.root.left.right.right = Node(8)
tree.root.left.right.right.right = Node(4)
tree.root.left.left = Node(1)
tree.root.right = Node(7)
tree.root.right.right = Node(2)
tree.root.right.right.right = Node(9)
print("\n Tree Nodes \n", end = "")
tree.preorder(tree.root)
print("\n", end = "")
# Test Case
tree.sum_of_level(4)
tree.sum_of_level(1)
tree.sum_of_level(3)
tree.sum_of_level(5)
tree.sum_of_level(2)

if __name__ == "__main__": main()``````

#### Output

`````` Tree Nodes
20   5   1   3   10   8   4   7   2   9
Level  4  node sum is :  27
Level  1  node sum is :  20
Level  3  node sum is :  6
Level  5  node sum is :  4
Level  2  node sum is :  12``````
``````#     Ruby Program
#     Sum of all nodes at given level in Binary Tree

#  Binary Tree node
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :left, :right

def initialize(data)
#  Set node value
self.data = data
self.left = nil
self.right = nil
end

end

class BinaryTree
# Define the accessor and reader of class BinaryTree
attr_accessor :root

def initialize()
# Set initial tree root to null
self.root = nil
end

# Display pre order elements
def preorder(node)
if (node != nil)
# Print node value
print("  ", node.data)
self.preorder(node.left)
self.preorder(node.right)
end

end

#  Find the sum of the nodes of at given level of the binary tree
def sum_level(node, level, k)
if (node == nil)
return 0
end

if (k == level)
return node.data
end

return self.sum_level(node.left, level, k + 1) + self.sum_level(node.right, level, k + 1)
end

#  Handles the request to find sum of nodes at given level
def sum_of_level(level)
if (level <= 0)
# Invalid level
return
elsif(self.root == nil)
print("\n Empty Tree\n")
else
result = self.sum_level(self.root, level, 1)
#  Display calculated result
print(" Level ", level ," node sum is : ", result ,"\n")
end

end

end

def main()
# Create tree object
tree = BinaryTree.new()
#
#         constructor binary tree
#         -----------------
#             20
#            /   \
#           5     7
#          / \     \
#         1   3     2
#            / \     \
#           10  8     9
#                \
#                 4
#         -----------------
#

tree.root = Node.new(20)
tree.root.left = Node.new(5)
tree.root.left.right = Node.new(3)
tree.root.left.right.left = Node.new(10)
tree.root.left.right.right = Node.new(8)
tree.root.left.right.right.right = Node.new(4)
tree.root.left.left = Node.new(1)
tree.root.right = Node.new(7)
tree.root.right.right = Node.new(2)
tree.root.right.right.right = Node.new(9)
print("\n Tree Nodes \n")
tree.preorder(tree.root)
print("\n")
# Test Case
tree.sum_of_level(4)
tree.sum_of_level(1)
tree.sum_of_level(3)
tree.sum_of_level(5)
tree.sum_of_level(2)
end

main()``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12
``````
``````/*
Scala Program
Sum of all nodes at given level in Binary Tree
*/

//  Binary Tree node
class Node(var data: Int , var left: Node , var right: Node)
{
def this(data: Int)
{
this(data, null, null);
}
}
class BinaryTree(var root: Node)
{
def this()
{
this(null);
}
// Display pre order elements
def preorder(node: Node): Unit = {
if (node != null)
{
// Print node value
print("  " + node.data);
preorder(node.left);
preorder(node.right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
def sum_level(node: Node, level: Int, k: Int): Int = {
if (node == null)
{
return 0;
}
if (k == level)
{
return node.data;
}
return sum_level(node.left, level, k + 1) + sum_level(node.right, level, k + 1);
}
//  Handles the request to find sum of nodes at given level
def sum_of_level(level: Int): Unit = {
// Invalid level
if (level <= 0)
{
return;
}
else if (this.root == null)
{
print("\n Empty Tree\n");
}
else
{
var result: Int = sum_level(this.root, level, 1);
//  Display calculated result
print(" Level " + level + " node sum is : " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Create tree object
var tree: BinaryTree = new BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
tree.root = new Node(20);
tree.root.left = new Node(5);
tree.root.left.right = new Node(3);
tree.root.left.right.left = new Node(10);
tree.root.left.right.right = new Node(8);
tree.root.left.right.right.right = new Node(4);
tree.root.left.left = new Node(1);
tree.root.right = new Node(7);
tree.root.right.right = new Node(2);
tree.root.right.right.right = new Node(9);
print("\n Tree Nodes \n");
tree.preorder(tree.root);
print("\n");
// Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
}
}``````

#### Output

`````` Tree Nodes
20  5  1  3  10  8  4  7  2  9
Level 4 node sum is : 27
Level 1 node sum is : 20
Level 3 node sum is : 6
Level 5 node sum is : 4
Level 2 node sum is : 12``````
``````/*
Swift 4 Program
Sum of all nodes at given level in Binary Tree
*/

//  Binary Tree node
class Node
{
var data: Int;
var left: Node? ;
var right: Node? ;
init(_ data: Int)
{
//  Set node value
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinaryTree
{
var root: Node? ;
init()
{
// Set initial tree root to null
self.root = nil;
}
// Display pre order elements
func preorder(_ node: Node? )
{
if (node != nil)
{
// Print node value
print("  ", node!.data, terminator: "");
self.preorder(node!.left);
self.preorder(node!.right);
}
}
//  Find the sum of the nodes of at given level of the binary tree
func sum_level(_ node: Node? , _ level : Int, _ k: Int)->Int
{
if (node == nil)
{
return 0;
}
if (k == level)
{
return node!.data;
}
return self.sum_level(node!.left, level, k + 1) + self.sum_level(node!.right, level, k + 1);
}
//  Handles the request to find sum of nodes at given level
func sum_of_level(_ level: Int)
{
// Invalid level
if (level <= 0)
{
return;
}
else if (self.root == nil)
{
print("\n Empty Tree\n", terminator: "");
}
else
{
let result: Int = self.sum_level(self.root, level, 1);
//  Display calculated result
print(" Level ", level ," node sum is : ", result ,"\n", terminator: "");
}
}
}
func main()
{
// Create tree object
let tree: BinaryTree = BinaryTree();
/*
constructor binary tree
-----------------
20
/   \
5     7
/ \     \
1   3     2
/ \     \
10  8     9
\
4
-----------------
*/
tree.root = Node(20);
tree.root!.left = Node(5);
tree.root!.left!.right = Node(3);
tree.root!.left!.right!.left = Node(10);
tree.root!.left!.right!.right = Node(8);
tree.root!.left!.right!.right!.right = Node(4);
tree.root!.left!.left = Node(1);
tree.root!.right = Node(7);
tree.root!.right!.right = Node(2);
tree.root!.right!.right!.right = Node(9);
print("\n Tree Nodes \n", terminator: "");
tree.preorder(tree.root);
print("\n", terminator: "");
// Test Case
tree.sum_of_level(4);
tree.sum_of_level(1);
tree.sum_of_level(3);
tree.sum_of_level(5);
tree.sum_of_level(2);
}
main();``````

#### Output

`````` Tree Nodes
20   5   1   3   10   8   4   7   2   9
Level  4  node sum is :  27
Level  1  node sum is :  20
Level  3  node sum is :  6
Level  5  node sum is :  4
Level  2  node sum is :  12``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.