Recursively search nested lists

前端 未结 1 1210
时光说笑
时光说笑 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<AccessibleTreeItem> children;
    
        public AccessibleTreeItem()
        {
            children = new List<AccessibleTreeItem>();
        }
    
        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)
提交回复
热议问题