思路:递归将树的左右节点交换
代码:
/**
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) {
mirror(root);
}
static void mirror(TreeNode root){
if(root == null){
return;
}
TreeNode temp = root.right;
root.right = root.left;
root.left = temp;
mirror(root.right);
mirror(root.left);
}
}end
本文介绍了一种通过递归方法实现二叉树节点左右子树互换的技术,即镜像翻转。该方法首先判断当前节点是否为空,若不为空则交换左右子节点,并递归地对新的左右子节点进行相同操作。
172万+

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



