Updated collapseTree to handle null at root.