Skip to content

Commit 329f0cd

Browse files
committed
feat: add solutions to lcof problem: No.27
1 parent cdf099e commit 329f0cd

File tree

6 files changed

+102
-72
lines changed

6 files changed

+102
-72
lines changed

lcof/面试题27. 二叉树的镜像/README.md

+76-49
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737

3838
## 解法
3939

40+
**方法一:递归**
41+
42+
我们先判断根节点是否为空,如果为空,直接返回空。如果不为空,我们交换根节点的左右子树,然后递归地交换左子树和右子树。
43+
44+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。最坏情况下,二叉树退化为链表,递归深度为 $n$,因此系统使用 $O(n)$ 大小的栈空间。
45+
4046
<!-- tabs:start -->
4147

4248
### **Python3**
@@ -60,6 +66,25 @@ class Solution:
6066
return root
6167
```
6268

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+
6388
### **Java**
6489

6590
```java
@@ -74,7 +99,9 @@ class Solution:
7499
*/
75100
class Solution {
76101
public TreeNode mirrorTree(TreeNode root) {
77-
if (root == null) return null;
102+
if (root == null) {
103+
return null;
104+
}
78105
TreeNode t = root.left;
79106
root.left = root.right;
80107
root.right = t;
@@ -85,26 +112,29 @@ class Solution {
85112
}
86113
```
87114

88-
### **JavaScript**
115+
### **C++**
89116

90-
```js
117+
```cpp
91118
/**
92119
* 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+
* };
101126
*/
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+
}
108138
};
109139
```
110140
@@ -120,43 +150,40 @@ var mirrorTree = function (root) {
120150
* }
121151
*/
122152
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
130160
}
131161
```
132162

133-
### **C++**
163+
### **JavaScript**
134164

135-
```cpp
165+
```js
136166
/**
137167
* 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+
* }
144172
*/
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;
159180
}
181+
const { left, right } = root;
182+
root.left = right;
183+
root.right = left;
184+
mirrorTree(left);
185+
mirrorTree(right);
186+
return root;
160187
};
161188
```
162189

@@ -247,11 +274,11 @@ impl Solution {
247274
public class Solution {
248275
public TreeNode MirrorTree(TreeNode root) {
249276
if (root == null) {
250-
return null;
277+
return root;
251278
}
252-
TreeNode tmp = root.left;
279+
TreeNode t = root.left;
253280
root.left = root.right;
254-
root.right = tmp;
281+
root.right = t;
255282
MirrorTree(root.left);
256283
MirrorTree(root.right);
257284
return root;

lcof/面试题27. 二叉树的镜像/Solution.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
88
* };
99
*/
10-
1110
class Solution {
1211
public:
1312
TreeNode* mirrorTree(TreeNode* root) {
14-
if (nullptr == root) {
15-
return nullptr;
13+
if (!root) {
14+
return root;
1615
}
17-
16+
swap(root->left, root->right);
1817
mirrorTree(root->left);
1918
mirrorTree(root->right);
20-
std::swap(root->left, root->right);
21-
2219
return root;
2320
}
24-
};
21+
};

lcof/面试题27. 二叉树的镜像/Solution.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
public class Solution {
1111
public TreeNode MirrorTree(TreeNode root) {
1212
if (root == null) {
13-
return null;
13+
return root;
1414
}
15-
TreeNode tmp = root.left;
15+
TreeNode t = root.left;
1616
root.left = root.right;
17-
root.right = tmp;
17+
root.right = t;
1818
MirrorTree(root.left);
1919
MirrorTree(root.right);
2020
return root;

lcof/面试题27. 二叉树的镜像/Solution.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func mirrorTree(root *TreeNode) *TreeNode {
10-
if root == nil {
11-
return root
12-
}
13-
root.Left, root.Right = root.Right, root.Left
14-
mirrorTree(root.Left)
15-
mirrorTree(root.Right)
16-
return root
9+
func mirrorTree(root *TreeNode) *TreeNode {
10+
if root == nil {
11+
return root
12+
}
13+
root.Left, root.Right = root.Right, root.Left
14+
mirrorTree(root.Left)
15+
mirrorTree(root.Right)
16+
return root
1717
}

lcof/面试题27. 二叉树的镜像/Solution.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
*/
1010
class Solution {
1111
public TreeNode mirrorTree(TreeNode root) {
12-
if (root == null) return null;
12+
if (root == null) {
13+
return null;
14+
}
1315
TreeNode t = root.left;
1416
root.left = root.right;
1517
root.right = t;

lcof/面试题27. 二叉树的镜像/Solution.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
* @return {TreeNode}
1111
*/
1212
var mirrorTree = function (root) {
13-
if (!root) return null;
14-
[root.left, root.right] = [root.right, root.left];
15-
mirrorTree(root.left);
16-
mirrorTree(root.right);
13+
if (!root) {
14+
return null;
15+
}
16+
const { left, right } = root;
17+
root.left = right;
18+
root.right = left;
19+
mirrorTree(left);
20+
mirrorTree(right);
1721
return root;
1822
};

0 commit comments

Comments
 (0)