title: Sum Nodes K Th Level Tree Represented String
signature: class Solution {
  
      public Integer sumNodesKThLevelTreeRepresentedString(String tree, Integer k) {
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;
  }
question_info.extension: java
question_info.entry_fn_name: sumNodesKThLevelTreeRepresentedString
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 14, "inputs": {"tree": "(0(5(6()())(4()(9()())))(7(1()())(3()())))", "k": 2}}, {"idx": 1, "outputs": 9, "inputs": {"tree": "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", "k": 3}}, {"idx": 2, "outputs": 12, "inputs": {"tree": "(0(5(6()())(4()(9()())))(7(1()())(3()())))", "k": 1}}, {"idx": 3, "outputs": 16, "inputs": {"tree": "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", "k": 2}}, {"idx": 4, "outputs": 0, "inputs": {"tree": "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", "k": 4}}, {"idx": 5, "outputs": 0, "inputs": {"tree": "(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", "k": 100}}, {"idx": 6, "outputs": 9, "inputs": {"tree": "(0(5(6()())(4()(9()())))(7(1()())(3()())))", "k": 3}}, {"idx": 7, "outputs": 0, "inputs": {"tree": "(0(5(6()())(4()(9()())))(7(1()())(3()())))", "k": 0}}, {"idx": 8, "outputs": 0, "inputs": {"tree": "0010", "k": 12}}, {"idx": 9, "outputs": 0, "inputs": {"tree": "kjtdgmy", "k": 97}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(String tree, Integer k, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(tree, k,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 2, 14);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 3, 9);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 1, 12);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 2, 16);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 4, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution("(8(3(2()())(6(5()())()))(5(10()())(7(13()())())))", 100, 0);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 3, 9);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution("(0(5(6()())(4()(9()())))(7(1()())(3()())))", 0, 0);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution("0010", 12, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution("kjtdgmy", 97, 0);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(String tree, Integer k, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(tree, k),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__