optimize depth_first_traversal