plugins – Is there is any way to add validation for sub categories in backend while publishing posts

My requirement is while submitting posts, If I selected a parent category that has a child category, I need to add validation, I need to select only child category if it has a parent category. For those categories which don’t have child categories, we can select parent categories.

Is there is any way to add validation for this