I have the following class:
public class Category {
public string Name { get; set; }
public Category Parent { get; set; }
public string Url { get; set; }
public List<Category> Children { get; set; }
}
Now given an instance of a Category and url. I wish to get the Category where the url matches the category or any of it’s children (or their children etc).
Therefore my function would have the following signature:
public Category FindCategory(Category category, string url);
I know recursion is the way to go and i have managed to come up with a solution. However i’ve definitely seen it done better but i can’t find where. I’d appreciate it if someone could show me the easiest and cleanest way to achieve this.
Thanks
Here is a simple recursive algorithm: