Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
binary-tree-cameras.cpp 917 Bytes
Copy Edit Raw Blame History
kamyu authored 2018-12-30 14:22 +08:00 . Update binary-tree-cameras.cpp
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minCameraCover(TreeNode* root) {
int result = 0;
if (dfs(root, &result) == UNCOVERED) {
++result;
}
return result;
}
private:
int dfs(TreeNode* root, int *result) {
int left = root->left ? dfs(root->left, result) : COVERED;
int right = root->right ? dfs(root->right, result) : COVERED;
if (left == UNCOVERED || right == UNCOVERED) {
++(*result);
return CAMERA;
}
if (left == CAMERA || right == CAMERA) {
return COVERED;
}
return UNCOVERED;
}
enum NODE {
UNCOVERED, COVERED, CAMERA
};
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search