废话不多说,代码呈上来:
class BinaryTree{
int value;
BinaryTree mLeft;
BinaryTree mRight;
}
public class BinaryTreePreOrderPrint {
public static void preOrderPrint(BinaryTree binaryTree) {
if(binaryTree==null)
return;
System.out.println(binaryTree.value);
preOrderPrint(binaryTree.mLeft);
preOrderPrint(binaryTree.mRight);
}
public static void mirrorRecursivelyPreOrderPrint(BinaryTree binaryTree) {
if(binaryTree==null)
return;
if(binaryTree.mLeft==null&&binaryTree.mRight==null)
{
if(binaryTree==null)
return;
else {
System.out.println(binaryTree.value);
return;
}
}
System.out.println(binaryTree.value);
swapNode(binaryTree);
mirrorRecursivelyPreOrderPrint(binaryTree.mLeft);
mirrorRecursivelyPreOrderPrint(binaryTree.mRight);
}
public static BinaryTree swapNode(BinaryTree binaryTree) {
BinaryTree tree=new BinaryTree();
tree=binaryTree.mLeft;
binaryTree.mLeft=binaryTree.mRight;
binaryTree.mRight=tree;
return binaryTree;
}
public static void main(String[] args) {
BinaryTree binaryTree=new BinaryTree();
binaryTree.value=10;
BinaryTree binaryTree1=new BinaryTree();
binaryTree1.value=9;
binaryTree1.mLeft=null;
BinaryTree binaryTree2=new BinaryTree();
binaryTree2.value=11;
binaryTree.mLeft=binaryTree1;
binaryTree.mRight=binaryTree2;
// mirrorRecursivelyPreOrderPrint(binaryTree);
//测试空树
BinaryTree treeNull=null;
if(treeNull==null)
System.out.println(treeNull+"treeNull为null");
mirrorRecursivelyPreOrderPrint(treeNull);
}
}