剑指offer第19题 输出二叉树的镜像 java解法
2016-07-24 10:15
531 查看
废话不多说,代码呈上来:
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);
}
}
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);
}
}
相关文章推荐
- 剑指offer中,求输入整数的二进制形式中1的个数(Java代码)
- 剑指offer面试题11 数值的整数次方的Java代码实现
- 剑指offer面试题12 使用BigInteger来实现 打印1到最大的n位数
- 剑指offer第12题 最优解
- 剑指offer 面试题23 从上往下打印二叉树 Java实现
- 剑指offer面试题 22 栈的压入、弹出序列的匹配
- 剑指offer 面试题21 包含min函数的栈 Java实现
- **HTML_DOM** DOM简介
- css伪类选择器详细解析及案例使用-----伪类选择器(1)
- CSS3-2
- CSS3-1
- 剑指offer 38题 【知识迁移能力】数字在排序数组中出现的次数
- 一个前端的自我修养
- JavaScript学习小结之使用canvas画“哆啦A梦”时钟
- js之变量的定义
- javascript权威指南--对象到原始值的转换(未完成)
- servlet、session、jsp的生命周期
- html编写随笔(二)
- window环境下搭建react native及相关插件
- window环境下搭建react native及相关插件