文字列のリンク リストを返すメソッドを作成しようとしています。ツリーがあり、ツリー内の各ノードには文字が格納されています。メソッドは、ツリーを介してすべての可能なパスを見つけることになっています。各パスは、リストに追加される文字列を作成します。
2 番目の for ループ内に問題があるようで、私には理解できません。このメソッドは、最初の if ステートメントで追加された文字のみを返します。
各ノードには、子ノードのリンクされたリストである変数 childList と、ノードが格納している文字である nodevalue が含まれています。
public LinkedList<String> findStrings() {
LinkedList<String> paths = new LinkedList<String>();
//add character to list if there are no children
if (childList.isEmpty()){
paths.add("" + nodevalue);
return paths;
}
//use recursion to add paths from all children to the list
for (TreeNode t : childList){
paths.addAll(t.findStrings());
//add nodevalue to the beginning of all strings in the list
for (String s : paths){
s = nodevalue + s;
}
}
for (String s : paths) System.out.println(s); //for debugging
return paths;
}