二叉树的镜像
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
思路
使用递归
/**
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) {
TreeNode tmp = null;
if (root != null)
{
tmp = root.left;
root.left = root.right;
root.right = tmp;
if (root.left != null){
Mirror(root.left);
}
if (root.right != null){
Mirror(root.right);
}
}
}
}
本文介绍了一种通过递归方式实现二叉树镜像变换的方法。具体步骤包括:交换根节点的左右子节点,然后分别对左子树和右子树进行相同的操作,直至所有节点都被处理。
129

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



