The time complexity is O(N^2) and space complexity is O(N).
Solution 2: prefix sums.
For each of the paths from the root to a node, we can maintain the prefix sums we’ve seen so far. When we encounter a new node, we can quickly look up to see if current node's value + previous prefix sum - sum is already in the prefix sums. If so, we add that to the counter. Both the time and space complexity is O(N).
Comments