Skip to content

Commit 12fd98b

Browse files
ChunelFengjunfeng.fj
and
junfeng.fj
authored
更新了[lcof-27.二叉树的镜像]中的cpp版本题解 (#340)
* 更新了[lcof-27.二叉树的镜像]中的cpp版本题解 * 添加了[lcof-27.二叉树镜像]对应的readme文件 Co-authored-by: junfeng.fj <junfeng.fj@alibaba-inc.com>
1 parent 0e05469 commit 12fd98b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ func mirrorTree(root *TreeNode) *TreeNode {
129129
}
130130
```
131131

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+
132163
### **...**
133164

134165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
};

lcof/面试题55 - II. 平衡二叉树/Solution.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Solution {
3131
}
3232
}
3333

34-
return false; // 如果
34+
return false; // 如果有一处已经确定不是平衡二叉树了,则直接返回false
3535
}
3636

3737
bool isBalanced(TreeNode* root) {

0 commit comments

Comments
 (0)