/**
* 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) {
if(root==null)
return null;
TreeNode tmp=root.left;
root.left=mirrorTree(root.right);
root.right=mirrorTree(tmp);
return root;
}
}
时间复杂度O(n),n为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点
空间复杂度O(n),最差情况下(当二叉树退化为链表),递归时系统需使用O(N) 大小的栈空间。