I have a tree with n elements that store something like |id|parent_id|. I need to find the maximum depth of this tree. I need to do this in Ruby, but pseudo code can help me too.
I have a tree with n elements that store something like |id|parent_id| . I
Share
Assuming that
{id, parent_id}pairs are in a dictionary/map, you can find max depth using memoization technique:Main function (pseudocode):
Recursive depth function: