'Need help searching through nested lists C# [closed]

I need to implement the Search function so it will output the path to the searched "folder".

{

    public class Folder

    {
        public string root { get; set; }
        public List<Folder> children { get; set; }
    }

    static void Main(string[] args)

    {

        string searchString = "sub-folder3"; //Folder to be searched

        //Test input

        Folder input = new Folder

        {
            root = "folder",
            children = new List<Folder>
            {

                new Folder

                {
                    root = "sub-folder",
                    children = new List<Folder>()

                },

                new Folder

                {
                    root = "sub-folder2",
                    children = new List<Folder>

                    {

                        new Folder

                        {
                            root = "sub-folder31",
                            children = new List<Folder>()
                        },

                        new Folder

                        {
                            root ="sub-folder3",
                            children = new List<Folder>()
                        }
                    }
                },

                new Folder

                {
                    root ="sub-folder4",
                    children = new List<Folder>()
                    {

                        new Folder

                        {
                            root = "sub-sub-folder4",
                            children = new List<Folder>()
                        }

                    }

                },

                new Folder

                {
                    root ="useless",
                    children = new List<Folder>()
                }
            }
        };

        List<string> paths = Search(input, searchString);
        foreach (var path in paths)
            Console.WriteLine(path);

    }



    /// <summary>

    /// Returns array of full paths to searched folder

    /// </summary>

    /// <param name="input"></param>

    /// <param name="searchString"></param>

    /// <returns></returns>

    private static List<string> Search(Folder input, string searchString)

    {

        throw new NotImplementedException("Complete this function. You can add or remove arguments as you see fit, but function must return array of full paths to the searched folder (or folders)");


Solution 1:[1]

If I understand this correctly, you are looking to get all sub directories matching some search criteria for a given directory and put the full path in a list. This can be accomplished using recursion as follows:

public static List<string> Search(Folder input, string searchString)
{
    var paths = new List<string>();

    var currentPath = input.root;

    foreach (var child in input.children)
    {
        if (child.root == searchString)
        {
            var fullPath = Path.Combine(currentPath, child.root);
            paths.Add(fullPath);
        }

        var childPaths = Search(child, searchString);
        foreach (var path in childPaths)
        {
            var fullPath = Path.Combine(currentPath, path);
            paths.Add(fullPath);
        }            
    }

    return paths;
}

Running this on the example provided yields:

folder\sub-folder2\sub-folder3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1