rewrite preorder traversal using iterate