the similar logic as inorder and postorder