The problem statement is mentioned here.
Following is the first solution.
As can be seen, the time complexity
of the algorithm
is where is the number of nodes. The space complexity is .
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
No comments:
Post a Comment