resolved index error for empty tree in traverse()