18.二叉树-镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

package facehandjava.tree;

public class MirrorTree {
  

   
public static Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
       
Node J = new Node(8, null, null);
        Node H =
new Node(4, null, null);
        Node G =
new Node(2, null, null);
        Node F =
new Node(7, null, J);
        Node E =
new Node(5, H, null);
        Node D =
new Node(1, null, G);
        Node C =
new Node(9, F, null);
        Node B =
new Node(3, D, E);
        Node A =
new Node(6, B, C);
       
return A;   //返回根节点
   
}

   
public static void main(String[] args) {
//       L2RRecursiveBinaryTree tree = new L2RRecursiveBinaryTree();
       
Node root = MirrorTree.init();
        System.
out.println("原型树从左到右打印");
        System.
out.println("639157248");
        System.
out.println("镜像树从左到右打印");
        MirrorTree(root);
        L2RRecursiveBinaryTree.L2RRecursiveBinaryTree(root);
    }

   
public static void MirrorTree(Node node) {
       
if (node== null) {
           
return;
        }
       
if (node!= null) {
            Node temp =node.getRightNode();
            node.setRightNode(node.getLeftNode());
            node.setLeftNode(temp);
        }
        MirrorTree(node.getLeftNode());
        MirrorTree(node.getRightNode());
    }

}

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值