Skip to content

Commit dc72ada

Browse files
Merge pull request youngyangyang04#1259 from xiaofei-2020/greed24
添加(0968.监控二叉树.md):增加typescript版本
2 parents fce0809 + 9da0958 commit dc72ada

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

problems/0968.监控二叉树.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,35 @@ var minCameraCover = function(root) {
476476
};
477477
```
478478

479+
### TypeScript
480+
481+
```typescript
482+
function minCameraCover(root: TreeNode | null): number {
483+
/** 0-无覆盖, 1-有摄像头, 2-有覆盖 */
484+
type statusCode = 0 | 1 | 2;
485+
let resCount: number = 0;
486+
if (recur(root) === 0) resCount++;
487+
return resCount;
488+
function recur(node: TreeNode | null): statusCode {
489+
if (node === null) return 2;
490+
const left: statusCode = recur(node.left),
491+
right: statusCode = recur(node.right);
492+
let resStatus: statusCode = 0;
493+
if (left === 0 || right === 0) {
494+
resStatus = 1;
495+
resCount++;
496+
} else if (left === 1 || right === 1) {
497+
resStatus = 2;
498+
} else {
499+
resStatus = 0;
500+
}
501+
return resStatus;
502+
}
503+
};
504+
```
505+
479506
### C
507+
480508
```c
481509
/*
482510
**函数后序遍历二叉树。判断一个结点状态时,根据其左右孩子结点的状态进行判断

0 commit comments

Comments
 (0)