You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution {
// 0 represent no camera cover, 1 represent camera, 2 represent camera cover
int ans = 0;
public int minCameraCover(TreeNode root) {
return postorder(root) == 0 ? ans + 1 : ans;
}
private int postorder(TreeNode root) {
if (root == null) {
return 2;
}
int left = postorder(root.left);
int right = postorder(root.right);
if (left == 0 || right == 0) {
++ans;
return 1;
} else if (left == 1 || right == 1) {
return 2;
} else {
return 0;
}
}
}
这是一个种树状DP的类型题目。
普通算法模型是:
类似题目:
The text was updated successfully, but these errors were encountered: