递归函数找不到节点并且 returns null

recursive function could not find the node and returns null

我有以下 treeview:

-node1 //ID=1
    -node1.1 //ID=1/1
    -node1.2
-node2
    -node2.1
        -node2.1.1 //ID = 2/1/1
    -node2.2
    -node2.3
-node3
    -node3.1
        -node3.1.1
        -node3.1.2 //ID = 3/1/2
    -node3.2
        -node3.2.1

我用的"ID"就是child的顺序,已经在node的Tag中了。例如,我想查找节点 -node3.1.2 并且我正在使用带有 itemId = "3/1/2" 的节点,我已经通过 Tag.

传递了它

这是我的代码:

public TreeNode FromID(string itemId, TreeNode rootNode)
{
    foreach (TreeNode node in rootNode.Nodes)
    {
        if (node.Tag != null)
        {
            var value = node.Tag as NodeTag;
            string Node_ID = value.NodeID;
            if (Node_ID.Equals(itemId)) return node;
            TreeNode next = FromID(itemId, node);
            if (next != null) return next;
        }
    }
    return null;
}

当我是 运行 代码时,它总是 null 作为输出。我很困惑为什么我有它?我可以请你帮忙吗?

经过多次测试,我找到了正确的答案。感谢您的意见,这对我帮助很大。因为当IF不正确时它失败了,所以我也添加了else部分并发现是正确的。

.....
else
{
    TreeNode nodeChild = FindTn(node.Nodes, NodeID);
    if (nodeChild != null) return nodeChild;
}