I'm working on a Path method that returns the path from the given node to the node with the key of a given value. My code returns the correct numbers, but they are inside a bracket. How do I remove the bracket?
private boolean pathhelp(Node n, int val, ArrayList<Integer> lst){
if(n == null){
return false;
}else if(val < n.key){
lst.add(n.key);
return pathhelp(n.left, val, lst);
}else if(val > n.key ){
lst.add(n.key);
return pathhelp(n.right, val, lst);
}else{
lst.add(n.key);
return true;
}
}
public String path(Node node, int value) {
ArrayList<Integer> path = new ArrayList<Integer>();
if(pathhelp(root,value,path) == true ){
System.out.println(path);
String p = "";
//build p from path list
return p;
}else{
return "";
}
}
}
The actual output is:
[6, 5, 1, 4]
But it is supposed to be:
6, 5, 1, 4