Sum of nodes at k-th level in a tree represented as string
Last Updated :
20 Dec, 2022
Given an integer ‘K’ and a binary tree in string format. Every node of a tree has a value in the range of 0 to 9. We need to find the sum of elements at the K-th level from the root. The root is at level 0.
Tree is given in the form: (node value(left subtree)(right subtree))
Examples:
Input : tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"
k = 2
Output : 14
Its tree representation is shown below
Elements at level k = 2 are 6, 4, 1, 3
sum of the digits of these elements = 6+4+1+3 = 14
Input : tree = "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))"
k = 3
Output : 9
Elements at level k = 3 are 5, 1 and 3
sum of digits of these elements = 5+1+3 = 9
1. Input 'tree' in string format and level k
2. Initialize level = -1 and sum = 0
3. for each character 'ch' in 'tree'
3.1 if ch == '(' then
--> level++
3.2 else if ch == ')' then
--> level--
3.3 else
if level == k then
sum = sum + (ch-'0')
4. Print sum
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int sumAtKthLevel(string tree, int k)
{
int level = -1;
int sum = 0;
int n = tree.length();
for ( int i=0; i<n; i++)
{
if (tree[i] == '(' )
level++;
else if (tree[i] == ')' )
level--;
else
{
if (level == k)
sum += (tree[i]- '0' );
}
}
return sum;
}
int main()
{
string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2;
cout << sumAtKthLevel(tree, k);
return 0;
}
|
Java
class GfG {
static int sumAtKthLevel(String tree, int k)
{
int level = - 1 ;
int sum = 0 ;
int n = tree.length();
for ( int i= 0 ; i<n; i++)
{
if (tree.charAt(i) == '(' )
level++;
else if (tree.charAt(i) == ')' )
level--;
else
{
if (level == k)
sum += (tree.charAt(i)- '0' );
}
}
return sum;
}
public static void main(String[] args)
{
String tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2 ;
System.out.println(sumAtKthLevel(tree, k));
}
}
|
Python3
def sumAtKthLevel(tree, k) :
level = - 1
sum = 0
n = len (tree)
for i in range (n):
if (tree[i] = = '(' ) :
level + = 1
else if (tree[i] = = ')' ):
level - = 1
else :
if (level = = k) :
sum + = ( ord (tree[i]) - ord ( '0' ))
return sum
if __name__ = = '__main__' :
tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"
k = 2
print (sumAtKthLevel(tree, k))
|
C#
using System;
class GfG {
static int sumAtKthLevel( string tree, int k)
{
int level = -1;
int sum = 0;
int n = tree.Length;
for ( int i = 0; i < n; i++)
{
if (tree[i] == '(' )
level++;
else if (tree[i] == ')' )
level--;
else
{
if (level == k)
sum += (tree[i]- '0' );
}
}
return sum;
}
public static void Main()
{
string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2;
Console.Write(sumAtKthLevel(tree, k));
}
}
|
Javascript
<script>
function sumAtKthLevel(tree, k)
{
let level = -1;
let sum = 0;
let n = tree.length;
for (let i = 0; i < n; i++)
{
if (tree[i] == '(' )
level++;
else if (tree[i] == ')' )
level--;
else
{
if (level == k)
sum += (tree[i] - '0' );
}
}
return sum;
}
let tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
let k = 2;
document.write(sumAtKthLevel(tree, k));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Recursive Method: The idea is to treat the string as tree without actually creating one, and simply traverse the string recursively in Postorder Fashion and consider nodes that are at level k only.
Following is the implementation of the same:
C++
#include <bits/stdc++.h>
using namespace std;
int sumAtKthLevel(string tree, int k, int &i, int level)
{
if (tree[i++]== '(' )
{
if (tree[i] == ')' )
return 0;
int sum=0;
if (level == k)
sum = tree[i]- '0' ;
int leftsum = sumAtKthLevel(tree,k,++i,level+1);
int rightsum = sumAtKthLevel(tree,k,++i,level+1);
++i;
return sum+leftsum+rightsum;
}
}
int main()
{
string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2;
int i=0;
cout << sumAtKthLevel(tree, k,i,0);
return 0;
}
|
Java
class GFG
{
static int i;
static int sumAtKthLevel(String tree, int k, int level)
{
if (tree.charAt(i++) == '(' )
{
if (tree.charAt(i) == ')' )
return 0 ;
int sum = 0 ;
if (level == k)
sum = tree.charAt(i) - '0' ;
++i;
int leftsum = sumAtKthLevel(tree, k, level + 1 );
++i;
int rightsum = sumAtKthLevel(tree, k, level + 1 );
++i;
return sum + leftsum + rightsum;
}
return Integer.MIN_VALUE;
}
public static void main(String[] args)
{
String tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2 ;
i = 0 ;
System.out.print(sumAtKthLevel(tree, k, 0 ));
}
}
|
Python
def sumAtKthLevel(tree, k, i, level):
if (tree[i[ 0 ]] = = '(' ):
i[ 0 ] + = 1
if (tree[i[ 0 ]] = = ')' ):
return 0
sum = 0
if (level = = k):
sum = int (tree[i[ 0 ]])
i[ 0 ] + = 1
leftsum = sumAtKthLevel(tree, k, i, level + 1 )
i[ 0 ] + = 1
rightsum = sumAtKthLevel(tree, k, i, level + 1 )
i[ 0 ] + = 1
return sum + leftsum + rightsum
tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"
k = 2
i = [ 0 ]
print (sumAtKthLevel(tree, k, i, 0 ))
|
C#
using System;
class GFG
{
static int i;
static int sumAtKthLevel(String tree, int k, int level)
{
if (tree[i++] == '(' )
{
if (tree[i] == ')' )
return 0;
int sum = 0;
if (level == k)
sum = tree[i] - '0' ;
++i;
int leftsum = sumAtKthLevel(tree, k, level + 1);
++i;
int rightsum = sumAtKthLevel(tree, k, level + 1);
++i;
return sum + leftsum + rightsum;
}
return int .MinValue;
}
public static void Main(String[] args)
{
String tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
int k = 2;
i = 0;
Console.Write(sumAtKthLevel(tree, k, 0));
}
}
|
Javascript
<script>
function sumAtKthLevel(tree,k,level)
{
if (tree[i++] == '(' )
{
if (tree[i] == ')' )
return 0;
let sum = 0;
if (level == k)
sum = tree[i] - '0' ;
++i;
let leftsum = sumAtKthLevel(tree, k, level + 1);
++i;
let rightsum = sumAtKthLevel(tree, k, level + 1);
++i;
return sum + leftsum + rightsum;
}
return Number.MIN_VALUE;
}
let tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
let k = 2;
let i = 0;
document.write(sumAtKthLevel(tree, k, 0));
</script>
|
Time Complexity: O(n), the time complexity of this algorithm is O(n) as we need to traverse all the nodes of the tree in order to get the sum of digits of elements at the kth level.
Auxiliary Space: O(1), If we consider the recursive call stack then it will be O(K).
Please Login to comment...