我正在尝试使用前序遍历在由字符 az 和 AZ 组成的二叉树中找到一个节点,其中向左标记为“0”,向右标记为“1”,以便正确对于左侧两个分支的节点,输出看起来像“00”。节点未排序。
到目前为止,我有这个:
static String routeNum = "";
private static String onePath(BinaryNodeInterface<Character> root, String route) {
BinaryNodeInterface<Character> temp = root;
if(temp != null){
if(temp.hasLeftChild()){
routeNum = onePath(temp.getLeftChild(),route+"0");
}
if(temp.hasRightChild()){
routeNum = onePath(temp.getRightChild(), route+"1");
}
}
System.out.print(route);
return route;
}
输出表明我正在到达正确的节点,但它不打印路径。