剑指Offer-镜像二叉树

package 剑指Offer.二叉树.二叉树镜像;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @program:多线程和IO
 * @descripton:输入一个二叉树,利用一个函数输出其镜像
 * @author:ZhengCheng
 * @create:2021/10/19-18:54
 **/
public class MirrorTree {
    public static void main(String[] args) throws Exception{
        //创建一颗二叉树,我们可以用层次遍历来验证我们的想法
        //init
        Node node4 = new Node("4");
        Node node2 = new Node("2");
        Node node7 = new Node("7");
        Node node1 = new Node("1");
        Node node3 = new Node("3");
        Node node6 = new Node("6");
     //  Node node9 = new Node("9");
        node4.left = node2;
        node4.right = node7;
        node2.left = node1;
        node2.right = node3;
      //  node7.right = node9;
        node7.left = node6;
        MirrorTree m = new MirrorTree();
        m.stepOut(node4);
        m.mirrorTree2(node4);
        m.stepOut(node4);
    }
    private void mirrorTree1 (Node head){
        //递归,没想出来。
    }
    private void mirrorTree2 (Node head){
        //使用栈的方法。类比层次遍历  On On
        LinkedList<Node> list = new LinkedList<>();
        Node temp = head;
        list.add(temp);
        while (!list.isEmpty()){
            Node poll = list.poll();
            if (poll.left != null){
                list.add(poll.left);
            }
            if (poll.right != null){
                list.add(poll.right);
            }
            //交换
            Node swap = poll.left;
            poll.left = poll.right;
            poll.right = swap;
        }

    }
    //correct
    private void stepOut (Node head)throws Exception{
        Node temp = head;
        Queue<Node> q = new LinkedList<>();
        q.add(temp);

        while (!q.isEmpty()){
            Node poll = q.poll();
            System.out.print(poll);
            if (poll.left != null ){
                q.add(poll.left);
                //消除nullpointerException
            }else if (!poll.getId().equals("null")&& (poll.left!=null || poll.right != null)){
                q.add(new Node("null"));
            }
            if (poll.right != null){
                q.add(poll.right);
            }else if (!poll.getId().equals("null") && (poll.left!=null || poll.right != null)){
                q.add(new Node("null"));
            }
        }
        System.out.println();
    }
}
class Node {
    public Node left ;
    public Node right ;
    private String id;

    @Override
    public String toString() {
        return "" +
                id +"-";
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Node(String id) {
        this.id = id;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值