Product of nodes at k-th level in a tree represented as string using Recursion
Last Updated :
25 Sep, 2022
Prerequisite: Product of nodes at k-th level in a tree represented as string
Given an integer ‘K’ and a binary tree in string format. Every node of a tree has value in range from 0 to 9. We need to find product of elements at K-th level from the root. The root is at level 0.
Note: 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: 72
Explanation:
Its tree representation is shown below
Elements at level k = 2 are 6, 4, 1, 3
product of these elements = 6 * 4 * 1 * 3 = 72
Input: Tree = “(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))”
k = 3
Output: 15
Elements at level k = 3 are 5, 1 and 3
product of these elements = 5 * 1 * 3 = 15
Approach: The idea is to treat the string as a tree without actually creating one, and simply traverse the string recursively in Postorder Fashion and consider nodes that are at level k only.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int productAtKthLevel(string tree,
int k, int & i, int level){
if (tree[i++] == '(' ) {
if (tree[i] == ')' )
return 1;
int product = 1;
if (level == k)
product = tree[i] - '0' ;
int leftproduct = productAtKthLevel(
tree, k, ++i, level + 1);
int rightproduct = productAtKthLevel(
tree, k, ++i, level + 1);
++i;
return product * leftproduct *
rightproduct;
}
}
int main()
{
string tree = "(0(5(6()())(4()"
"(9()())))(7(1()())(3()())))" ;
int k = 2;
int i = 0;
cout << productAtKthLevel(tree, k, i, 0);
return 0;
}
|
Java
class GFG {
static int i;
static int productAtKthLevel(
String tree, int k, int level){
if (tree.charAt(i++) == '(' ) {
if (tree.charAt(i) == ')' )
return 1 ;
int product = 1 ;
if (level == k)
product = tree.charAt(i) - '0' ;
++i;
int leftproduct = productAtKthLevel(
tree, k, level + 1 );
++i;
int rightproduct = productAtKthLevel(
tree, k, level + 1 );
++i;
return product * leftproduct
* rightproduct;
}
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(
productAtKthLevel(tree, k, 0 )
);
}
}
|
Python
def productAtKthLevel(tree, k, i, level):
if (tree[i[ 0 ]] = = '(' ):
i[ 0 ] + = 1
if (tree[i[ 0 ]] = = ')' ):
return 1
product = 1
if (level = = k):
product = int (tree[i[ 0 ]])
i[ 0 ] + = 1
leftproduct = productAtKthLevel(tree,
k, i, level + 1 )
i[ 0 ] + = 1
rightproduct = productAtKthLevel(tree,
k, i, level + 1 )
i[ 0 ] + = 1
return product * leftproduct * rightproduct
if __name__ = = "__main__" :
tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"
k = 2
i = [ 0 ]
print (productAtKthLevel(tree, k, i, 0 ))
|
C#
using System;
class GFG {
static int i;
static int productAtKthLevel(
String tree, int k, int level){
if (tree[i++] == '(' ) {
if (tree[i] == ')' )
return 1;
int product = 1;
if (level == k)
product = tree[i] - '0' ;
++i;
int leftproduct = productAtKthLevel(
tree, k, level + 1);
++i;
int rightproduct =
productAtKthLevel(tree, k, level + 1);
++i;
return product *
leftproduct * rightproduct;
}
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(productAtKthLevel(tree, k, 0));
}
}
|
Javascript
<script>
var i;
function productAtKthLevel( tree, k, level){
if (tree[i++] == '(' ) {
if (tree[i] == ')' )
return 1;
var product = 1;
if (level == k)
product = tree[i] - '0' ;
++i;
var leftproduct = productAtKthLevel(
tree, k, level + 1);
++i;
var rightproduct =
productAtKthLevel(tree, k, level + 1);
++i;
return product *
leftproduct * rightproduct;
}
return int.MinValue;
}
var tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))" ;
var k = 2;
i = 0;
document.write(productAtKthLevel(tree, k, 0));
</script>
|
Time Complexity: O(N)
Auxiliary space: O(N), for recursive stack space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...