mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge pull request #1259 from xiaofei-2020/greed24
添加(0968.监控二叉树.md):增加typescript版本
This commit is contained in:
@ -476,7 +476,35 @@ var minCameraCover = function(root) {
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function minCameraCover(root: TreeNode | null): number {
|
||||
/** 0-无覆盖, 1-有摄像头, 2-有覆盖 */
|
||||
type statusCode = 0 | 1 | 2;
|
||||
let resCount: number = 0;
|
||||
if (recur(root) === 0) resCount++;
|
||||
return resCount;
|
||||
function recur(node: TreeNode | null): statusCode {
|
||||
if (node === null) return 2;
|
||||
const left: statusCode = recur(node.left),
|
||||
right: statusCode = recur(node.right);
|
||||
let resStatus: statusCode = 0;
|
||||
if (left === 0 || right === 0) {
|
||||
resStatus = 1;
|
||||
resCount++;
|
||||
} else if (left === 1 || right === 1) {
|
||||
resStatus = 2;
|
||||
} else {
|
||||
resStatus = 0;
|
||||
}
|
||||
return resStatus;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
/*
|
||||
**函数后序遍历二叉树。判断一个结点状态时,根据其左右孩子结点的状态进行判断
|
||||
|
Reference in New Issue
Block a user