File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change 71
71
72
72
后序遍历代码如下:
73
73
74
- ```
74
+ ``` CPP
75
75
int traversal (TreeNode* cur) {
76
76
77
77
// 空节点,该节点有覆盖
@@ -124,7 +124,7 @@ int traversal(TreeNode* cur) {
124
124
125
125
代码如下:
126
126
127
- ```
127
+ ```CPP
128
128
// 空节点,该节点有覆盖
129
129
if (cur == NULL) return 2;
130
130
```
@@ -143,7 +143,7 @@ if (cur == NULL) return 2;
143
143
144
144
代码如下:
145
145
146
- ```
146
+ ``` CPP
147
147
// 左右节点都有覆盖
148
148
if (left == 2 && right == 2 ) return 0 ;
149
149
```
@@ -163,7 +163,7 @@ left == 2 && right == 0 左节点覆盖,右节点无覆盖
163
163
此时摄像头的数量要加一,并且return 1,代表中间节点放摄像头。
164
164
165
165
代码如下:
166
- ```
166
+ ``` CPP
167
167
if (left == 0 || right == 0 ) {
168
168
result++;
169
169
return 1;
@@ -180,7 +180,7 @@ left == 1 && right == 1 左右节点都有摄像头
180
180
181
181
代码如下:
182
182
183
- ```
183
+ ``` CPP
184
184
if (left == 1 || right == 1 ) return 2 ;
185
185
```
186
186
@@ -198,7 +198,7 @@ if (left == 1 || right == 1) return 2;
198
198
199
199
所以递归结束之后,还要判断根节点,如果没有覆盖,result++,代码如下:
200
200
201
- ```
201
+ ``` CPP
202
202
int minCameraCover (TreeNode* root) {
203
203
result = 0;
204
204
if (traversal(root) == 0) { // root 无覆盖
You can’t perform that action at this time.
0 commit comments