title: Product Nodes K Th Level Tree Represented String
signature: int productNodesKThLevelTreeRepresentedString(string tree, int k) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(string tree, int k, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(tree, k), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 2, 72); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 3, 15); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 1, 35); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 2, 0); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 4, 1); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 100, 1); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 3, 9); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 0, 0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver("0010", 12, 1); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver("kjtdgmy", 97, 1); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def product_nodes_k_th_level_tree_represented_string(tree, k):
      level = (- 1)
      product = 1
      n = len(tree)
      for i in range(0, n):
          if (tree[i] == '('):
              level += 1
          elif (tree[i] == ')'):
              level -= 1
          elif (level == k):
              product *= (int(tree[i]) - int('0'))
      return product
source_cpp: int product_nodes_k_th_level_tree_represented_string(string tree, int k) {
    int level = - 1;
    int product = 1;
    int n = tree . length ( );
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( tree [ i ] == '(' ) level ++;
      else if ( tree [ i ] == ')' ) level --;
      else {
        if ( level == k ) product *= ( tree [ i ] - '0' );
      }
    }
    return product;
  }