Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da559f0

Browse files
committed
添加 0968.监控二叉树.md Scala版本
1 parent a031937 commit da559f0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎problems/0968.监控二叉树.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,5 +544,40 @@ int minCameraCover(struct TreeNode* root){
544544
}
545545
```
546546
547+
### Scala
548+
549+
```scala
550+
object Solution {
551+
def minCameraCover(root: TreeNode): Int = {
552+
var result = 0
553+
def traversal(cur: TreeNode): Int = {
554+
// 空节点,该节点有覆盖
555+
if (cur == null) return 2
556+
var left = traversal(cur.left)
557+
var right = traversal(cur.right)
558+
// 情况1,左右节点都有覆盖
559+
if (left == 2 && right == 2) {
560+
return 0
561+
}
562+
// 情况2
563+
if (left == 0 || right == 0) {
564+
result += 1
565+
return 1
566+
}
567+
// 情况3
568+
if (left == 1 || right == 1) {
569+
return 2
570+
}
571+
-1
572+
}
573+
574+
if (traversal(root) == 0) {
575+
result += 1
576+
}
577+
result
578+
}
579+
}
580+
```
581+
547582
-----------------------
548583
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /