牛客网-二叉树的镜像

简介: 牛客网-二叉树的镜像

题目描述

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

输入描述:

二叉树的镜像定义:源二叉树

8

/

6 10

/ \ /

5 7 9 11

镜像二叉树

8

/

10 6

/ \ /

11 9 7 5

牛客网OJ连接:https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011

力扣连接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/submissions/

解题

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if(root==null)
            return;
        //就是一个交换的过程
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        Mirror(root.left);
        Mirror(root.right);
    }
}
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root==null)
            return root;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }
}
目录
相关文章
【剑指offer】-二叉树的镜像-18/67
【剑指offer】-二叉树的镜像-18/67
|
10月前
【一刷《剑指Offer》】面试题 19:二叉树的镜像
【一刷《剑指Offer》】面试题 19:二叉树的镜像
|
10月前
剑指 Offer 27:二叉树的镜像
剑指 Offer 27:二叉树的镜像
42 0
|
10月前
|
存储 算法 C++
六六力扣刷题二叉树之基础
六六力扣刷题二叉树之基础
89 0
leetcode105/1382-构建二叉树与二叉平衡树(人生苦短,速速递归)
leetcode105/1382-构建二叉树与二叉平衡树(人生苦短,速速递归)
leetcode105/1382-构建二叉树与二叉平衡树(人生苦短,速速递归)
|
C++
剑指Offer - 面试题27:二叉树的镜像
剑指Offer - 面试题27:二叉树的镜像
79 0
剑指offer 26. 二叉树的镜像
剑指offer 26. 二叉树的镜像
44 0
图解LeetCode——剑指 Offer 27. 二叉树的镜像
图解LeetCode——剑指 Offer 27. 二叉树的镜像
98 0
|
Python
LeetCode 剑指 Offer 27. 二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
98 0
|
Java Python
【LeetCode每日一题】剑指 Offer 27. 二叉树的镜像(持续更新)
【LeetCode每日一题】剑指 Offer 27. 二叉树的镜像(持续更新)
78 0
【LeetCode每日一题】剑指 Offer 27. 二叉树的镜像(持续更新)