public void inOrderTraversal(TreeNode node) {
if(node == null) {
return;
}
inOrderTraversal(node.getLeft());
System.out.print(node.getKey() + " ");
inOrderTraversal(node.getRight());
}
I wrote this code for going through a binary search tree and printing out the items in order from low to high. it works just fine but i need to get it to return a String instead of just printing them out so that it will work with an applet im using. however when i change the return type to String it wont work unless i add another return outside of the if()???
public String inOrderTraversal(TreeNode node) {
String w = "";
if(node == null) {
return w;
}
inOrderTraversal(node.getLeft());
w += node.getKey() + " ";
inOrderTraversal(node.getRight());
}
something closer to this is what i need but it wont work this way? anyone who could help explain to me how to make this work. Any suggestions would be greatly appreciated!