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

题解
/**
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);
}
}
本文介绍了一种将二叉树转换为其镜像的算法实现。通过递归方式交换二叉树节点的左右子树,实现了二叉树的镜像变换。代码使用Java语言编写,展示了如何定义二叉树节点类和实现镜像变换的方法。
240

被折叠的 条评论
为什么被折叠?



