37
37
38
38
## 解法
39
39
40
+ ** 方法一:递归**
41
+
42
+ 我们先判断根节点是否为空,如果为空,直接返回空。如果不为空,我们交换根节点的左右子树,然后递归地交换左子树和右子树。
43
+
44
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。最坏情况下,二叉树退化为链表,递归深度为 $n$,因此系统使用 $O(n)$ 大小的栈空间。
45
+
40
46
<!-- tabs:start -->
41
47
42
48
### ** Python3**
@@ -60,6 +66,25 @@ class Solution:
60
66
return root
61
67
```
62
68
69
+ ``` python
70
+ # Definition for a binary tree node.
71
+ # class TreeNode:
72
+ # def __init__(self, x):
73
+ # self.val = x
74
+ # self.left = None
75
+ # self.right = None
76
+
77
+ class Solution :
78
+ def mirrorTree (self , root : TreeNode) -> TreeNode:
79
+ if root is None :
80
+ return root
81
+ left = self .mirrorTree(root.left)
82
+ right = self .mirrorTree(root.right)
83
+ root.left = right
84
+ root.right = left
85
+ return root
86
+ ```
87
+
63
88
### ** Java**
64
89
65
90
``` java
@@ -74,7 +99,9 @@ class Solution:
74
99
*/
75
100
class Solution {
76
101
public TreeNode mirrorTree (TreeNode root ) {
77
- if (root == null ) return null ;
102
+ if (root == null ) {
103
+ return null ;
104
+ }
78
105
TreeNode t = root. left;
79
106
root. left = root. right;
80
107
root. right = t;
@@ -85,26 +112,29 @@ class Solution {
85
112
}
86
113
```
87
114
88
- ### ** JavaScript **
115
+ ### ** C++ **
89
116
90
- ``` js
117
+ ``` cpp
91
118
/* *
92
119
* Definition for a binary tree node.
93
- * function TreeNode(val) {
94
- * this.val = val;
95
- * this.left = this.right = null;
96
- * }
97
- */
98
- /**
99
- * @param {TreeNode} root
100
- * @return {TreeNode}
120
+ * struct TreeNode {
121
+ * int val;
122
+ * TreeNode *left;
123
+ * TreeNode *right;
124
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
125
+ * };
101
126
*/
102
- var mirrorTree = function (root ) {
103
- if (! root) return null ;
104
- [root .left , root .right ] = [root .right , root .left ];
105
- mirrorTree (root .left );
106
- mirrorTree (root .right );
107
- return root;
127
+ class Solution {
128
+ public:
129
+ TreeNode* mirrorTree(TreeNode* root) {
130
+ if (!root) {
131
+ return root;
132
+ }
133
+ swap(root->left, root->right);
134
+ mirrorTree(root->left);
135
+ mirrorTree(root->right);
136
+ return root;
137
+ }
108
138
};
109
139
```
110
140
@@ -120,43 +150,40 @@ var mirrorTree = function (root) {
120
150
* }
121
151
*/
122
152
func mirrorTree(root *TreeNode) *TreeNode {
123
- if root == nil {
124
- return root
125
- }
126
- root.Left , root.Right = root.Right , root.Left
127
- mirrorTree (root.Left )
128
- mirrorTree (root.Right )
129
- return root
153
+ if root == nil {
154
+ return root
155
+ }
156
+ root.Left, root.Right = root.Right, root.Left
157
+ mirrorTree(root.Left)
158
+ mirrorTree(root.Right)
159
+ return root
130
160
}
131
161
```
132
162
133
- ### ** C++ **
163
+ ### ** JavaScript **
134
164
135
- ``` cpp
165
+ ``` js
136
166
/**
137
167
* Definition for a binary tree node.
138
- * struct TreeNode {
139
- * int val;
140
- * TreeNode *left;
141
- * TreeNode *right;
142
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
143
- * };
168
+ * function TreeNode(val) {
169
+ * this.val = val;
170
+ * this.left = this.right = null;
171
+ * }
144
172
*/
145
-
146
- class Solution {
147
- public:
148
- TreeNode* mirrorTree(TreeNode* root) {
149
- // 后序遍历
150
- if (nullptr == root) {
151
- return nullptr;
152
- }
153
-
154
- mirrorTree(root->left);
155
- mirrorTree(root->right);
156
- std::swap(root->left, root->right);
157
-
158
- return root;
173
+ /**
174
+ * @param {TreeNode} root
175
+ * @return {TreeNode}
176
+ */
177
+ var mirrorTree = function (root ) {
178
+ if (! root) {
179
+ return null ;
159
180
}
181
+ const { left , right } = root;
182
+ root .left = right;
183
+ root .right = left;
184
+ mirrorTree (left);
185
+ mirrorTree (right);
186
+ return root;
160
187
};
161
188
```
162
189
@@ -247,11 +274,11 @@ impl Solution {
247
274
public class Solution {
248
275
public TreeNode MirrorTree (TreeNode root ) {
249
276
if (root == null ) {
250
- return null ;
277
+ return root ;
251
278
}
252
- TreeNode tmp = root .left ;
279
+ TreeNode t = root .left ;
253
280
root .left = root .right ;
254
- root .right = tmp ;
281
+ root .right = t ;
255
282
MirrorTree (root .left );
256
283
MirrorTree (root .right );
257
284
return root ;
0 commit comments