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

Lines changed: 76 additions & 49 deletions
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

Lines changed: 4 additions & 7 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 8 additions & 4 deletions
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)