这种递归方法应该这样做:
static TreeNode[] GetNodes(IEnumerable<string> items, string prefix = "")
{
int preLen = prefix.Length;
// items that match the current prefix and have a nonzero character right after
// the prefix
var candidates = items.Where(i => i.Length > preLen &&
i[preLen] != '0' &&
i.StartsWith(prefix));
// create nodes from candidates that have a 0 two characters after the prefix.
// their child nodes are recursively generated from the candidate list
return candidates.Where(i => i.Length > preLen + 1 && i[preLen + 1] == '0')
.Select(i =>
new TreeNode(i, GetNodes(candidates, prefix + i[preLen])))
.ToArray();
}
你可以像这样调用它:
treeView.Nodes.AddRange(GetNodes(lst));