gpt4 book ai didi

c# - 查找 Deepest child Treenode 的层级

转载 作者:太空狗 更新时间:2023-10-30 00:44:15 24 4
gpt4 key购买 nike

我有树节点,我想找到树节点中最深的子节点。如果有 2 个子节点分别具有 level 11level 13 那么我需要 unction 返回值 13。

我该怎么做?

public int FindLevel(TreeNode oParentNode)
{
counter++;
forech(TreeNode oSubNode in oParentNode.Nodes)
{
FindLevel(oParentNode);
}

return Counter;
}

最佳答案

这是我给你的建议:

private int GetDeepestChildNodeLevel(TreeNode node)
{
var subLevel = node.Nodes.Cast<TreeNode>().Select(GetDeepestChildNodeLevel);
return subLevel.Count() == 0 ? 1 : subLevel.Max() + 1;
}

这里有显式类型:

private int GetDeepestChildNodeLevel(TreeNode node)
{
var subLevel = node.Nodes.Cast<TreeNode>().Select<TreeNode, int>(subNode => GetDeepestChildNodeLevel(subNode));
return subLevel.Count<int>() == 0 ? 1 : subLevel.Max() + 1;
}

关于c# - 查找 Deepest child Treenode 的层级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8310601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com