假设我有这棵树:
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/