I am trying to recursively display the path taken to a given node in a binary tree where the method will output the path needed in the following way: "left, right, left". Here is what I have so far:
public static void pathToNode(BTNode p, char target, String res){
if(p.data == target){
res = res + p.data;
System.out.println(res);
return;
}else if(res != null){
if(res.charAt(0) == 'S'){
res = res + p.data;
}
}else{
pathToNode(p.leftLink, target, res);
pathToNode(p.leftLink, target, res);
}
}
This code is intended to just print out the path like so: "ABCD". Having done this I intend on making the method print out either left of right based on the correct option for each node traversal. Any Ideas?