static Node closestNodeUsingMorrisTraversal ( Node root , int key ) {
  int diff = Integer . MAX_VALUE ;
  Node curr = root ;
  Node closest = null ;
  while ( curr != null ) {
    if ( curr . left == null ) {
      if ( diff > Math . abs ( curr . data - key ) ) {
        diff = Math . abs ( curr . data - key ) ;
        closest = curr ;
      }
      curr = curr . right ;
    }
    else {
      Node pre = curr . left ;
      while ( pre . right != null && pre . right != curr ) pre = pre . right ;
      if ( pre . right == null ) {
        pre . right = curr ;
        curr = curr . left ;
      }
      else {
        pre . right = null ;
        if ( diff > Math . abs ( curr . data - key ) ) {
          diff = Math . abs ( curr . data - key ) ;
          closest = curr ;
        }
        curr = curr . right ;
      }
    }
  }
  return closest ;
}

static Node closestNodeUsingMorrisTraversal ( Node root , int key ) { int diff = Integer . MAX_VALUE ; Node curr = root ; Node closest = null ; while ( curr != null ) { if ( curr . left == null ) { if ( diff > Math . abs ( curr . data - key ) ) { diff = Math . abs ( curr . data - key ) ; closest = curr ; } curr = curr . right ; } else { Node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; if ( diff > Math . abs ( curr . data - key ) ) { diff = Math . abs ( curr . data - key ) ; closest = curr ; } curr = curr . right ; } } } return closest ; }
(method_declaration (modifiers) type: (type_identifier) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (identifier))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (null_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (null_literal))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))))) alternative: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal)) right: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))) body: (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) consequence: (block (expression_statement (assignment_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))))) alternative: (block (expression_statement (assignment_expression left: (field_access object: (identifier) field: (identifier)) right: (null_literal))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (field_access object: (identifier) field: (identifier)))))))))) (return_statement (identifier))))