File tree 3 files changed +58
-1
lines changed
3 files changed +58
-1
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,37 @@ func mirrorTree(root *TreeNode) *TreeNode {
129
129
}
130
130
```
131
131
132
+ ### ** C++**
133
+
134
+ ``` cpp
135
+ /* *
136
+ * Definition for a binary tree node.
137
+ * struct TreeNode {
138
+ * int val;
139
+ * TreeNode *left;
140
+ * TreeNode *right;
141
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
142
+ * };
143
+ */
144
+
145
+ class Solution {
146
+ public:
147
+ TreeNode* mirrorTree(TreeNode* root) {
148
+ // 后续遍历
149
+ if (nullptr == root) {
150
+ return nullptr;
151
+ }
152
+
153
+ mirrorTree(root->left);
154
+ mirrorTree(root->right);
155
+ std::swap(root->left, root->right);
156
+
157
+ return root;
158
+ }
159
+ };
160
+
161
+ ```
162
+
132
163
### ** ...**
133
164
134
165
```
Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+
11
+ class Solution {
12
+ public:
13
+ TreeNode* mirrorTree (TreeNode* root) {
14
+ // 后续遍历
15
+ if (nullptr == root) {
16
+ return nullptr ;
17
+ }
18
+
19
+ mirrorTree (root->left );
20
+ mirrorTree (root->right );
21
+ std::swap (root->left , root->right );
22
+
23
+ return root;
24
+
25
+ }
26
+ };
Original file line number Diff line number Diff line change @@ -31,7 +31,7 @@ class Solution {
31
31
}
32
32
}
33
33
34
- return false ; // 如果
34
+ return false ; // 如果有一处已经确定不是平衡二叉树了,则直接返回false
35
35
}
36
36
37
37
bool isBalanced (TreeNode* root) {
You can’t perform that action at this time.
0 commit comments