@yukas66

Let me explain time complexity. It is O(nlogn) in general and O(n^2) in worst case.

In common case (a balanced tree):

1
2 3
4 5 6 7

Firstly, pathSum() will be called O(n) times, which is O(n) to traverse the tree.

Then, on every node, we call pathSumFrom(), but notice, the time of this call is different on different node. Consider node 2 and node 3 in above tree, they have O(3) and O(3) respectfully. Which means, the sum time of pathSumFrom() from a layer is <= O(n).

Overall, the time complexity is: O(n) + height * O(n)

Best case = O(n) + logn * O(n) = O(nlogn)

Worst case = O(n) + n * O(n) = O(n^2)

Correct me if I'm wrong.