Solution 1: accepted 2ms
Iterative method.
Time: O(n)
Space: O(n)
|
|
Solution 2: accepted 2ms
Divide and conquer recursive solution. Not good for this simple question, since the method consumes extra time, but it’s good for some more complicated questions.
Time complexity is O(n) as we visit each node only once.
Time: O(n)
Space: O(n)
|
|
Solution 3: accepted 2ms
Naive recursive solution, trivial.
Time: O(n)
Space: O(n)
|
|