Fix the tree depth logic to stop duplicate root elements being displayed