title: Sum Nodes K Th Level Tree Represented String
signature: sum_nodes_k_th_level_tree_represented_string <- function(tree, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(tree, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(tree, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          "(0(5(6()())(4()(9()())))(7(1()())(3()())))", 2L, 
          14L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 3L, 
          9L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          "(0(5(6()())(4()(9()())))(7(1()())(3()())))", 1L, 
          12L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 2L, 
          16L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 4L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 100L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          "(0(5(6()())(4()(9()())))(7(1()())(3()())))", 3L, 
          9L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          "(0(5(6()())(4()(9()())))(7(1()())(3()())))", 0L, 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          "0010", 12L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          "kjtdgmy", 97L, 
          0L
      )))
  
  }
  
  main()
source_py: def sum_nodes_k_th_level_tree_represented_string(tree, k):
      level = (- 1)
      sum = 0
      n = len(tree)
      for i in range(n):
          if (tree[i] == '('):
              level += 1
          elif (tree[i] == ')'):
              level -= 1
          elif (level == k):
              sum += (ord(tree[i]) - ord('0'))
      return sum
source_cpp: int sum_nodes_k_th_level_tree_represented_string(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;
  }