c# - 检查树路径中节点的最佳算法?

标签 c# algorithm path tree

假设我有这棵树:

           O-ROOT
         /        \
    O-A            O-B
     /          /       \
O-A1        O-B1        O-B2

我想在 C# 中执行此操作:

1. Check every node starting from root (I think the best way is trought recursion?);
2. If I found a node with value = "Hello", return true and STOP the searching function;

你能帮我制定最好的算法吗?

最佳答案

bool FindHello(Node node)
{
    if (node.Content == "Hello")
        return true;
    foreach (Node c in node.Children)
        if (FindHello(c))
            return true;
    return false;
}

关于c# - 检查树路径中节点的最佳算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10119102/

相关文章:

algorithm - 数组 A 的 A.heapsize 和 A.length 之间的区别以及示例

c++ - 算法库排序100万个0到1 float 的测时耗时

neo4j - 安装后出现问题请选择neo4j中要存储应用程序数据的路径

windows - Windows 10中路径变量的默认值

path - 如何在贝塞尔路径上进行几何高级操作?

c# - 使用 OpenXMLWriter 时 Excel 中的日期格式问题

c# - 将 List<KeyValuePair> 绑定(bind)到组合框

c# - 随机移动物体而不在墙内 - Unity 5

c# - 修改属性后列出顺序更改

ruby - 如何从方法内部获取方法的返回值?