二叉树的镜像(简单)
2020年8月23日
题目来源:力扣
解题
这道题跟之前有道翻转二叉树是一样的
这次再写递归,思路也一致
/**
* 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) {
//虚假头节点
TreeNode fake_root=root;
return recur(fake_root);
}
private TreeNode recur(TreeNode root){
if(root==null) return root;
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
recur(root.left);
recur(root.right);
return root;
}
}