reduce time complexity O(n²) to O(n)

the solution can be optimized by calculating the treeDepth in the same recursion  rather than calling a treeDepth() function separately.