Issue
the code itself is working though whenever I try to print the results which are integers of the following form: 1 2 3 4 5 I end up with an extra whitespace after the 5, how can I delete the last whitespace while keeping the integers separated on one line
public void inorder() {
inrec(root);
}
private void inrec(BTNode<E> root) {
if (root == null) {
return;
}
inrec(root.getLeft());
System.out.printf(root.getData()+" ");
inrec(root.getRight());
}
Solution
Try this.
public void inorder() {
inrec(root, "", "");
}
private void inrec(Node<E> root, String prefix, String suffix) {
if (root == null)
return;
System.out.print(prefix);
inrec(root.getLeft(), "", " ");
System.out.print(root.getData());
inrec(root.getRight(), " ", "");
System.out.print(suffix);
}
If you change inorder()
to
public void inorder() {
inrec(root, "(", ")");
}
It will print (1 2 3 4 5)
.
Answered By - 英語は苦手
Answer Checked By - Katrina (JavaFixing Volunteer)