refactor the code of check bst validity.