剑指offer-面试题19-二叉树的镜像

package case19_MirrorOfBinaryTree;

public class MirrorOfBinaryTree {
	
	//完成二叉树的镜像操作
	public static void MirrorTree(BinaryTreeNode root) {
		BinaryTreeNode tmpNode = null;
		if (root == null) {
			return;
		}
		if (root.lchild == null && root.rchild == null)
			return;
		tmpNode = root.lchild;
		root.lchild = root.rchild;
		root.rchild = tmpNode;
		MirrorTree(root.lchild);
		MirrorTree(root.rchild);
	}
	//*****TestCase start*****//
	//先序遍历二叉树
	public static void printTree(BinaryTreeNode btn){
		if(btn == null)
			return;
		System.out.print(btn.data + " ");
		printTree(btn.lchild);
		printTree(btn.rchild);
	}
	/**
	 * 二叉树的镜像
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
	   //       8
       //    /    \
       //   6     10
       //  / \   / \
       // 5   7 9  11
       BinaryTreeNode root = new BinaryTreeNode();
       root.data = 8;
       root.lchild = new BinaryTreeNode();
       root.lchild.data = 6;
       root.lchild.lchild = new BinaryTreeNode();
       root.lchild.lchild.data = 5;
       root.lchild.rchild = new BinaryTreeNode();
       root.lchild.rchild.data = 7;
       root.rchild = new BinaryTreeNode();
       root.rchild.data = 10;
       root.rchild.lchild = new BinaryTreeNode();
       root.rchild.lchild.data = 9;
       root.rchild.rchild = new BinaryTreeNode();
       root.rchild.rchild.data = 11;
       printTree(root);
       System.out.println();
       MirrorTree(root);
       printTree(root);
       //         1
       //        /
       //       3
       //      /
       //     5
       //    /
       //   7
       //  /
       // 9
       BinaryTreeNode root2 = new BinaryTreeNode();
       root2.data = 1;
       root2.lchild = new BinaryTreeNode();
       root2.lchild.data = 3;
       root2.lchild.lchild = new BinaryTreeNode();
       root2.lchild.lchild.data = 5;
       root2.lchild.lchild.lchild = new BinaryTreeNode();
       root2.lchild.lchild.lchild.data = 7;
       root2.lchild.lchild.lchild.lchild = new BinaryTreeNode();
       root2.lchild.lchild.lchild.lchild.data = 9;
       System.out.println("\n");
       printTree(root2);
       System.out.println();
       MirrorTree(root2);
       printTree(root2);

       // 0
       //  \
       //   2
       //    \
       //     4
       //      \
       //       6
       //        \
       //         8
       BinaryTreeNode root3 = new BinaryTreeNode();
       root3.data = 0;
       root3.rchild = new BinaryTreeNode();
       root3.rchild.data = 2;
       root3.rchild.rchild = new BinaryTreeNode();
       root3.rchild.rchild.data = 4;
       root3.rchild.rchild.rchild = new BinaryTreeNode();
       root3.rchild.rchild.rchild.data = 6;
       root3.rchild.rchild.rchild.rchild = new BinaryTreeNode();
       root3.rchild.rchild.rchild.rchild.data = 8;
       System.out.println("\n");
       printTree(root3);
       System.out.println();
       MirrorTree(root3);
       printTree(root3);
	}	
	//*****TestCase   end*****//
}



package case19_MirrorOfBinaryTree;

//构造二叉树节点
public class BinaryTreeNode {
	int data;
	BinaryTreeNode lchild;
	BinaryTreeNode rchild;
	public BinaryTreeNode(int data) {
		this.data = data;
	}
	public BinaryTreeNode(){
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值