我有一个树类,看起来像:
Class Tree {
Node root;
Node curNode;
public List<String> find(String value) {
if (curNode == null) curNode = root;
for (Node child : curNode.children) {
if (found == false) {
if (child.data.equals(value)) {
// if it finds it return the path to this node.
}
curNode = child;
findDFS(value);
}
}
}
class Node {
List<Node> children;
String data;
}
Run Code Online (Sandbox Code Playgroud)
树根包含指向其他子节点的子节点的指针等等.我遇到的问题是,一旦找到节点,我需要返回该节点的路径.