refactor isTree method