static int sumOfNodesAtNthLevel ( node root , int k ) {
  if ( root == null ) return 0 ;
  Queue < node > que = new LinkedList < > ( ) ;
  que . add ( root ) ;
  int level = 0 ;
  int sum = 0 ;
  int flag = 0 ;
  while ( ! que . isEmpty ( ) ) {
    int size = que . size ( ) ;
    while ( size -- > 0 ) {
      node ptr = que . peek ( ) ;
      que . remove ( ) ;
      if ( level == k ) {
        flag = 1 ;
        sum += ptr . data ;
      }
      else {
        if ( ptr . left != null ) que . add ( ptr . left ) ;
        if ( ptr . right != null ) que . add ( ptr . right ) ;
      }
    }
    level ++ ;
    if ( flag == 1 ) break ;
  }
  return sum ;
}

static int sumOfNodesAtNthLevel ( node root , int k ) { if ( root == null ) return 0 ; Queue < node > que = new LinkedList < > ( ) ; que . add ( root ) ; int level = 0 ; int sum = 0 ; int flag = 0 ; while ( ! que . isEmpty ( ) ) { int size = que . size ( ) ; while ( size -- > 0 ) { node ptr = que . peek ( ) ; que . remove ( ) ; if ( level == k ) { flag = 1 ; sum += ptr . data ; } else { if ( ptr . left != null ) que . add ( ptr . left ) ; if ( ptr . right != null ) que . add ( ptr . right ) ; } } level ++ ; if ( flag == 1 ) break ; } return sum ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (null_literal))) consequence: (return_statement (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))))) alternative: (block (if_statement condition: (parenthesized_expression (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (if_statement condition: (parenthesized_expression (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))))))) (expression_statement (update_expression (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (break_statement)))) (return_statement (identifier))))