预期(A (D () ()) (B (C () ()) ()))
得到A(D() ()) (B(C() ()) ())
我输了第一个和最后一个支架。
private String toString(BinaryNode curr) {
String str = "";
if(curr == null) {
return str;
} else{
str += curr.value;
str += "(" + toString(curr.left) + ") (" + toString(curr.right) + ")";
}
return str;
}
如果我更改了代码,我得到了(A((D() ())) ((B((C() ())) ())))
private String toString(BinaryNode curr) {
String str = "";
if(curr == null) {
return str;
} else{
str += curr.value;
str += "(" + toString(curr.left) + ") (" + toString(curr.right) + ")";
}
return "(" + str + ")";
}
答案 0 :(得分:0)
您需要确定代码的哪一部分由括号括起。返回的人似乎做了这个工作:
private String toString(BinaryNode curr) {
String str = "";
if(curr == null) {
return str;
} else{
str += curr.value+" ";
str += toString(curr.left) + toString(curr.right);
}
return "(" + str + ")";
}