Algorithms and Data Structures/Coding Practices
AlgoExpert Depth First Search (DFS)
brightlightkim
2022. 7. 12. 15:42
import java.util.*;
class Program {
// Do not edit the class below except
// for the depthFirstSearch method.
// Feel free to add new properties
// and methods to the class.
static class Node {
String name;
List<Node> children = new ArrayList<Node>();
public Node(String name) {
this.name = name;
}
public List<String> depthFirstSearch(List<String> array) {
array.add(name);
for (Node child : children){
if (child == null){
return array;
}
child.depthFirstSearch(array);
}
return array;
}
public Node addChild(String name) {
Node child = new Node(name);
children.add(child);
return this;
}
}
}