链接
牛客:二叉树的镜像
LeetCode:剑指 Offer 27. 二叉树的镜像
226. 翻转二叉树
思路
题目比较简单,直接交换左右子树,然后递归处理
代码
牛客:
public class Solution {
public void Mirror(TreeNode root) {
if(root == null)
return;
if(root.left == null && root.right == null)
return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);
}
}
LeetCode:
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
invertTree(root.right);
return root;
}
}