Recursively search nested lists

前端 未结 1 1208
时光说笑
时光说笑 2021-02-10 14:40

I\'ve read and searched and I\'m yet to figure out an answer to this relatively simple issue.

I have a class:

public class AccessibleTreeItem
{
    publ         


        
1条回答
  •  执笔经年
    2021-02-10 14:46

    public class AccessibleTreeItem
    {
        public string name;
        public List children;
    
        public AccessibleTreeItem()
        {
            children = new List();
        }
    
        public static AccessibleTreeItem Find(AccessibleTreeItem node, string name)
        {
    
            if (node == null)
                return null;
    
            if (node.name == name)
                return node;
    
            foreach (var child in node.children)
            {
                var found = Find(child, name);
                if (found != null)
                    return found;
            }
    
            return null;
        }
    }
    

    0 讨论(0)
提交回复
热议问题