Run this while loop till queue got empty
while ( ! Q . empty ( ) || ! S . empty ( ) ) { while ( ! S . empty ( ) ) { temp = S . top ( ) ; S . pop ( ) ; cout << temp -> data << " ▁ " ; if ( dir == LEFT ) { if ( temp -> left ) Q . push ( temp -> left ) ; if ( temp -> right ) Q . push ( temp -> right ) ; }