从树中返回值的字符串
Returning a String of values from a tree
好的,所以我想用中序、前序和后序遍历一棵树。我得到了遍历树的正确算法,但我需要 return 形式为 "Node1 Node2 Node3"
的字符串,但我有 "Node1Node2Node3"
。这是我的预购代码:
public String PreOrder() {
/* TODO: Implement */
StringBuilder string = new StringBuilder();
preorderRecursive(string, root);
return string.toString();
}
public StringBuilder preorderRecursive(StringBuilder string, Node current){
if (current != null) {
string.append(current.key);
preorderRecursive(string, current.left);
preorderRecursive(string, current.right);
}
return string;
}
我收到错误 org.junit.ComparisonFailure: expected:<3[ 1 2 8 4 5 7 9 ]10> but was:<3[1284579]10>
只需在 stringBuilder 中添加一个 space,
if (current != null) {
string.append(current.key).append(" ");
...
好的,所以我想用中序、前序和后序遍历一棵树。我得到了遍历树的正确算法,但我需要 return 形式为 "Node1 Node2 Node3"
的字符串,但我有 "Node1Node2Node3"
。这是我的预购代码:
public String PreOrder() {
/* TODO: Implement */
StringBuilder string = new StringBuilder();
preorderRecursive(string, root);
return string.toString();
}
public StringBuilder preorderRecursive(StringBuilder string, Node current){
if (current != null) {
string.append(current.key);
preorderRecursive(string, current.left);
preorderRecursive(string, current.right);
}
return string;
}
我收到错误 org.junit.ComparisonFailure: expected:<3[ 1 2 8 4 5 7 9 ]10> but was:<3[1284579]10>
只需在 stringBuilder 中添加一个 space,
if (current != null) {
string.append(current.key).append(" ");
...