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

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:

二叉树的镜像定义:源二叉树 
            8
           /  \
          6   10
         / \  / \
        5  7 9 11
        镜像二叉树
            8
           /  \
          10   6
         / \  / \
        11 9 7  5

思路:考察二叉树的遍历和镜像定义。
采用先序遍历,对两棵树进行分析。首先根结点相同,然后根结点的左右孩子恰好互换,左右孩子的左右孩子也互换,…,一直到叶子结点结束。

根据分析,可以用先序遍历,首先判断结点是不是叶子结点,如果不是交换其左右孩子,然后判断其左右孩子是不是叶子结点,如果不是再分别交换其左右孩子的左右孩子,…,递归下去,直到叶子结点,结束。

牛客网OJ代码如下:

/**
 * 
 */
package com.su.biancheng;

import java.util.Stack;

/**
 * @title MirrorTree.java
 * @author Shuai
 * @date 2016-4-25下午2:54:15
 */
public class MirrorTree {
    public static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }

    }
    //递归先序遍历
    public static void preOrderTarverse(TreeNode root){
        if(root==null)
            return;
        //打印根节点
        System.out.print(root.val+" ");
        //递归左右孩子
        if(root.left!=null){
            preOrderTarverse(root.left);
        }
        if(root.right!=null){
            preOrderTarverse(root.right);
        }
    }
    //递归
    public static void Mirror(TreeNode root) {
        if(root==null||(root.left==null&&root.right==null))
            return;
        //交换左右孩子
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
        //递归左右孩子
        if(root.left!=null){
            Mirror(root.left);
        }
        if(root.right!=null){
            Mirror(root.right);
        }
    }
    //非递归
    public static void Mirror2(TreeNode root) {
        if(root==null||(root.left==null&&root.right==null))
            return;
        Stack<TreeNode> s=new Stack<TreeNode>();
        s.push(root);
        while(!s.isEmpty()){
            TreeNode temp=s.pop();
            if(root.left==null&&root.right==null)
                return;
            TreeNode temp2=temp.left;
            temp.left=temp.right;
            temp.right=temp2;
            //遍历左子树
            if(temp.left!=null)
                s.push(temp.left);
            //遍历右子树
            if(temp.right!=null)
                s.push(temp.right);
        }
    }
    public static void preOrderTarverse2(TreeNode root){
        if(root==null)
            return;
        Stack<TreeNode> s=new Stack<TreeNode>();
        s.push(root);
        while(!s.isEmpty()){
            TreeNode temp=s.pop();
            while(temp!=null){
                System.out.print(temp.val+" ");
                s.push(temp.right);
                temp=temp.left;
            }
        }
    }

    public static void main(String[] args){
        TreeNode root = 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);

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

        Mirror(root);
        preOrderTarverse(root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值