Optimize category tree building