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 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
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 = 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
sum of digits of these elements = 5 * 1 * 3 = 15
Approach :
1. Input 'tree' in string format and level k
2. Initialize level = -1 and product = 1
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
product = product * (ch-'0')
4. Print product
C++
// C++ implementation to find product of // digits of elements at k-th level #include <bits/stdc++.h> using namespace std; // Function to find product of digits // of elements at k-th level int productAtKthLevel(string tree, int k) { int level = -1; int product = 1; // Initialize result int n = tree.length(); for (int i = 0; i < n; i++) { // increasing level number if (tree[i] == '(') level++; // decreasing level number else if (tree[i] == ')') level--; else { // check if current level is // the desired level or not if (level == k) product *= (tree[i] - '0'); } } // required product return product; } // Driver program int main() { string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"; int k = 2; cout << productAtKthLevel(tree, k); return 0; } |
chevron_right
filter_none
Java
// Java implementation to find product of // digits of elements at k-th level class GFG { // Function to find product of digits // of elements at k-th level static int productAtKthLevel(String tree, int k) { int level = -1; // Initialize result int product = 1; int n = tree.length(); for (int i = 0; i < n; i++) { // increasing level number if (tree.charAt(i) == '(') level++; // decreasing level number else if (tree.charAt(i) == ')') level--; else { // check if current level is // the desired level or not if (level == k) product *= (tree.charAt(i) - '0'); } } // required product return product; } // Driver program public static void main(String[] args) { String tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"; int k = 2; System.out.println(productAtKthLevel(tree, k)); } } // This code is contributed // by Smitha Dinesh Semwal. |
chevron_right
filter_none
Python3
# Python 3 implementation # to find product of # digits of elements # at k-th level # Function to find # product of digits # of elements at # k-th level def productAtKthLevel(tree, k): level = -1 # Initialize result product = 1 n = len(tree) for i in range(0, n): # increasing level number if (tree[i] == '('): level+=1 # decreasing level number elif (tree[i] == ')'): level-=1 else: # check if current level is # the desired level or not if (level == k): product *= (int(tree[i]) - int('0')) # required product return product # Driver program tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"k = 2 print(productAtKthLevel(tree, k)) # This code is contributed by # Smitha Dinesh Semwal |
chevron_right
filter_none
C#
// C# implementation to find // product of digits of // elements at k-th level using System; class GFG { // Function to find product // of digits of elements // at k-th level static int productAtKthLevel(string tree, int k) { int level = -1; // Initialize result int product = 1; int n = tree.Length; for (int i = 0; i < n; i++) { // increasing // level number if (tree[i] == '(') level++; // decreasing // level number else if (tree[i] == ')') level--; else { // check if current level is // the desired level or not if (level == k) product *= (tree[i] - '0'); } } // required product return product; } // Driver Code static void Main() { string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"; int k = 2; Console.WriteLine(productAtKthLevel(tree, k)); } } // This code is contributed by Sam007 |
chevron_right
filter_none
PHP
<?php // php implementation to find product of // digits of elements at k-th level // Function to find product of digits // of elements at k-th level function productAtKthLevel($tree, $k) { $level = -1; $product = 1; // Initialize result $n = strlen($tree); for ($i = 0; $i < $n; $i++) { // increasing level number if ($tree[$i] == '(') $level++; // decreasing level number else if ($tree[$i] == ')') $level--; else { // check if current level is // the desired level or not if ($level == $k) $product *= (ord($tree[$i]) - ord('0')); } } // required product return $product; } // Driver Code $tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))"; $k = 2; echo productAtKthLevel($tree, $k); //This code is contributed by mits ?> |
chevron_right
filter_none
Output:
72
Time Complexity: O(n)
Recommended Posts:
- Sum of nodes at k-th level in a tree represented as string
- Difference between sums of odd level and even level nodes of a Binary Tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Swap Nodes in Binary tree of every k'th level
- Count the number of nodes at given level in a tree using BFS.
- Find maximum level product in Binary Tree
- Count nodes with two children at level L in a Binary Tree
- Print nodes between two given level numbers of a binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Product of all nodes in a Binary Tree
- Product of all leaf nodes of 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.



