第15题:输入一棵二叉树,将该树转换为它的镜像


欢迎转载,转载请务必注明出处:http://blog.csdn.net/alading2009/article/details/45114413

第15题:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。* 要求: 使用递归和循环两种方法完成树的镜像转换。

这里写图片描述


代码

package test015;

import common.BTNode;
import common.CommonFunctions;

import java.util.ArrayList;

/**
 * Created by cq on 2015/4/18.
 * 第15题:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
 * 要求:  使用递归和循环两种方法完成树的镜像转换。
 */
public class Test015 {

    //递归方式实现的二叉搜索树镜像功能
    public static void mirrorBTreeRecursion(BTNode bTree){
        if (bTree == null || (bTree.getLeft() == null && bTree.getRight() == null)){
            return;
        }

        mirrorBTreeRecursion(bTree.getLeft());
        mirrorBTreeRecursion(bTree.getRight());

        BTNode temp = bTree.getLeft();
        bTree.setLeft(bTree.getRight());
        bTree.setRight(temp);
    }

    //循环方式实现的二叉搜索树镜像功能,类似于图的BFS搜索
    public static void mirrorBTreeLoop(BTNode bTree){
        if (bTree == null){
            return;
        }

        BTNode currentBTNode = null;
        //使用ArrayList建一个队列,保存还未被镜像的子树
        ArrayList<BTNode> unmirroredBTrees = new ArrayList<BTNode>();
        unmirroredBTrees.add(bTree);

        //只要存在未被镜像的子树,就继续循环
        while (!unmirroredBTrees.isEmpty()){
            //取出第一个子树
            currentBTNode = unmirroredBTrees.get(0);
            unmirroredBTrees.remove(0);

            BTNode curLeft = currentBTNode.getLeft();
            BTNode curRight = currentBTNode.getRight();

            //向队列中添加新发现的子树
            if (curLeft != null){
                unmirroredBTrees.add(curLeft);
            }
            if (curRight != null){
                unmirroredBTrees.add(curRight);
            }

            //左右交换
            currentBTNode.setLeft(curRight);
            currentBTNode.setRight(curLeft);
        }
    }

    public static void main(String[] args){
        BTNode bTree = new BTNode(8);
        BTNode node2 = new BTNode(6);
        BTNode node3 = new BTNode(10);
        BTNode node4 = new BTNode(5);
        BTNode node5 = new BTNode(7);
        BTNode node6 = new BTNode(9);
        BTNode node7 = new BTNode(11);
        bTree.setLeft(node2);
        bTree.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);
        node3.setRight(node7);

        System.out.print("使用递归方法镜像之前二叉搜索树的中序序列:");
        CommonFunctions.inorderTraversal(bTree);
        System.out.println();
        mirrorBTreeRecursion(bTree);
        System.out.print("使用递归方法镜像之后二叉搜索树的中序序列:");
        CommonFunctions.inorderTraversal(bTree);
        System.out.println();

        System.out.print("使用循环方法镜像之前二叉搜索树的中序序列:");
        CommonFunctions.inorderTraversal(bTree);
        System.out.println();
        mirrorBTreeLoop(bTree);
        System.out.print("使用循环方法镜像之后二叉搜索树的中序序列:");
        CommonFunctions.inorderTraversal(bTree);
        System.out.println();
    }
}




执行结果

Connected to the target VM, address: '127.0.0.1:2313', transport: 'socket'
使用递归方法镜像之前二叉搜索树的中序序列:5 6 7 8 9 10 11 
使用递归方法镜像之后二叉搜索树的中序序列:11 10 9 8 7 6 5 
使用循环方法镜像之前二叉搜索树的中序序列:11 10 9 8 7 6 5 
使用循环方法镜像之后二叉搜索树的中序序列:5 6 7 8 9 10 11 
Disconnected from the target VM, address: '127.0.0.1:2313', transport: 'socket'

Process finished with exit code 0
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值