【剑指Offer】面试题27:二叉树的镜像

这篇博客详细介绍了如何通过递归、广度优先遍历(BFS)和深度优先遍历(DFS)三种方法实现二叉树的镜像翻转。分别提供了前序遍历的递归实现、队列辅助的BFS以及栈辅助的DFS方法,每种方法都包括了清晰的代码示例。
摘要由CSDN通过智能技术生成

二叉树的遍历:
参考博客:
https://blog.csdn.net/DJames23/article/details/116191586
https://blog.csdn.net/weixin_44404255/article/details/104517916
https://blog.csdn.net/qq_44932835/article/details/104948804

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) {
        val = x;
    }
}
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 面试题27:二叉树的镜像
 * 题目:请完成一个函数,输入一棵二叉树,该函数输出它的镜像。
 * @author
 * @create 2021-04-26 10:09
 */
public class Solution27 {
    public static void main(String[] args) {
        TreeNode root1 = new TreeNode(8);
        TreeNode node1 = new TreeNode(6);
        TreeNode node2 = new TreeNode(10);
        TreeNode node3 = new TreeNode(5);
        TreeNode node4 = new TreeNode(7);
        TreeNode node5 = new TreeNode(9);
        TreeNode node6 = new TreeNode(11);

        root1.left = node1;
        root1.right = node2;
        node1.left = node3;
        node1.right = node4;
        node2.left = node5;
        node2.right = node6;

        TreeNode treeNode = mirrorRecursively(root1);
        System.out.println(treeNode);
    }

    /**
     * 方法一:递归,前序遍历的递归实现
     * @param root
     * @return
     */
    public static TreeNode mirrorRecursively(TreeNode root){
        if (root == null){
            //根节点为null就退出
            return root;
        }
        if (root.left == null && root.right == null){
            //如果根节点的左右子节点都为空,就不用交换
            return root;
        }
        //交换左右子节点
        TreeNode tempNode = root.left;
        root.left = root.right;
        root.right = tempNode;
        //如果左子节点不为空,就向左边递归
        if (root.left != null){
            mirrorRecursively(root.left);
        }
        if (root.right != null){
            //如果右子节点不为空,就向右边递归
            mirrorRecursively(root.right);
        }
        return root;
    }

/*=====================================================================*/
    /**
     * 方法二:广度优先遍历,遍历每一个节点,然后交换其左右子节点
     * @param root
     * @return
     */
    public static TreeNode mirrorBFS(TreeNode root){
        if (root == null){
            return root;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            //交换
            TreeNode tempNode = node.left;
            node.left = node.right;
            node.right = tempNode;

            if (node.left != null){
                queue.add(node.left);
            }
            if (node.right != null){
                queue.add(node.right);
            }
        }
        return root;
    }

    /**
     * 二叉树的广度优先遍历,队列
     * @param root
     * @return
     */
    public static void binaryTreeBFS(TreeNode root){
        if (root == null){
            //如果为空就返回
            return;
        }
        //使用队列
        Queue<TreeNode> queue = new LinkedList<>();
        //现将根节点加入到队列中
        queue.add(root);
        while (!queue.isEmpty()){
            //当队列不为空时,先取出队列头部元素
            TreeNode node = queue.poll();
            System.out.println(node);//打印
            //添加左节点
            if (node.left != null){
                queue.add(node.left);
            }
            //添加右节点
            if (node.right != null){
                queue.add(node.right);
            }
        }
    }

    /*=====================================================================*/
    /**
     * 方法三:深度优先遍历,前序遍历的非递归写法
     * @param root
     * @return
     */
    public static TreeNode mirrorDFS(TreeNode root){
        if (root == null){
            return root;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();

            TreeNode tempNode = node.left;
            node.left = node.right;
            node.right = tempNode;

            if (node.left != null){
                stack.push(node.left);
            }
            if (node.right != null){
                stack.push(node.right);
            }
        }
        return root;
    }


    /**
     * 二叉树的深度优先遍历,使用栈
     * @param root
     */
    public static void binaryTreeDFS(TreeNode root){
        if (root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            System.out.println(node);//先输出根节点
            //stack先进后出,所以先push右节点再push左节点
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }
        }

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值