static void spiralOrder ( Node root ) {
  Deque < Node > d = new LinkedList < Node > ( ) ;
  d . addLast ( root ) ;
  int dir = 0 ;
  while ( d . size ( ) > 0 ) {
    int size = d . size ( ) ;
    while ( size -- > 0 ) {
      if ( dir == 0 ) {
        Node temp = d . peekLast ( ) ;
        d . pollLast ( ) ;
        if ( temp . right != null ) d . addFirst ( temp . right ) ;
        if ( temp . left != null ) d . addFirst ( temp . left ) ;
        System . out . print ( temp . data + " " ) ;
      }
      else {
        Node temp = d . peekFirst ( ) ;
        d . pollFirst ( ) ;
        if ( temp . left != null ) d . addLast ( temp . left ) ;
        if ( temp . right != null ) d . addLast ( temp . right ) ;
        System . out . print ( temp . data + " " ) ;
      }
    }
    System . out . println ( ) ;
    dir = 1 - dir ;
  }
}

static void spiralOrder ( Node root ) { Deque < Node > d = new LinkedList < Node > ( ) ; d . addLast ( root ) ; int dir = 0 ; while ( d . size ( ) > 0 ) { int size = d . size ( ) ; while ( size -- > 0 ) { if ( dir == 0 ) { Node temp = d . peekLast ( ) ; d . pollLast ( ) ; if ( temp . right != null ) d . addFirst ( temp . right ) ; if ( temp . left != null ) d . addFirst ( temp . left ) ; System . out . print ( temp . data + " ▁ " ) ; } else { Node temp = d . peekFirst ( ) ; d . pollFirst ( ) ; if ( temp . left != null ) d . addLast ( temp . left ) ; if ( temp . right != null ) d . addLast ( temp . right ) ; System . out . print ( temp . data + " ▁ " ) ; } } System . out . println ( ) ; dir = 1 - dir ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier))) body: (block (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 (type_identifier))) 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))) (while_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) 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 (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (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: (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 (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (string_literal)))))) alternative: (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: (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 (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (string_literal))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (decimal_integer_literal) right: (identifier))))))))