solution to maxDepth