The problem statement is mentioned here.
Following is the first solution.
As can be seen, the time complexity of the algorithm is $O(N^2)$ where $N$ is the number of nodes. The space complexity is $O(1)$.
Following is the first solution.
initialize maxDepth to 0
for each node i from 0 to N-1
set parent to P[i]
set depth to 1
while parent is not -1
set parent to P[parent]
increment depth
end while
if maxDepth < depth
set maxDepth = depth
end if
end for
As can be seen, the time complexity of the algorithm is $O(N^2)$ where $N$ is the number of nodes. The space complexity is $O(1)$.
No comments:
Post a Comment