# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:defmirrorTree(self,root:TreeNode)->TreeNode:ifrootisNone:returnNoneroot.left,root.right=root.right,root.leftself.mirrorTree(root.left)self.mirrorTree(root.right)returnroot
1 2 3 4 5 6 7 8 910111213141516171819202122
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicTreeNodemirrorTree(TreeNoderoot){if(root==null){returnnull;}TreeNodet=root.left;root.left=root.right;root.right=t;mirrorTree(root.left);mirrorTree(root.right);returnroot;}}
1 2 3 4 5 6 7 8 9101112131415161718192021
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classSolution{public:TreeNode*mirrorTree(TreeNode*root){if(!root){returnroot;}swap(root->left,root->right);mirrorTree(root->left);mirrorTree(root->right);returnroot;}};
1 2 3 4 5 6 7 8 91011121314151617
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcmirrorTree(root*TreeNode)*TreeNode{ifroot==nil{returnroot}root.Left,root.Right=root.Right,root.LeftmirrorTree(root.Left)mirrorTree(root.Right)returnroot}
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {TreeNode} */varmirrorTree=function(root){if(!root){returnnull;}const{left,right}=root;root.left=right;root.right=left;mirrorTree(left);mirrorTree(right);returnroot;};
1 2 3 4 5 6 7 8 910111213141516171819202122
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */publicclassSolution{publicTreeNodeMirrorTree(TreeNoderoot){if(root==null){returnroot;}TreeNodet=root.left;root.left=root.right;root.right=t;MirrorTree(root.left);MirrorTree(root.right);returnroot;}}
1 2 3 4 5 6 7 8 910111213141516171819202122232425
/* public class TreeNode {* var val: Int* var left: TreeNode?* var right: TreeNode?* init(_ val: Int) {* self.val = val* self.left = nil* self.right = nil* }* }*/classSolution{funcmirrorTree(_root:TreeNode?)->TreeNode?{guardletroot=rootelse{returnnil}lettemp=root.leftroot.left=root.rightroot.right=temp_=mirrorTree(root.left)_=mirrorTree(root.right)returnroot}}