/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
//切换当前节点的灯的状态
void s(struct TreeNode* root){
if(root==NULL) return;
if(root->val==1) root->val=0;
else root->val=1;
}
//切换当前节点及其左右子节点的状态
void rs(struct TreeNode* root){
s(root);
s(root->left);
s(root->right);
}
//切换 以当前节点为根 的子树中,所有节点上的灯的状态
void fs(struct TreeNode* root){
if(root!=NULL){
s(root);
fs(root->left);
fs(root->right);
}
return;
}
int min(int a, int b, int c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c; // 由于只有三个数,如果前两个条件都不满足,c自然是最小的
}
}
int closeLampInTree(struct TreeNode* root){
if(root==NULL) return 0;
if(root->val==0){
return closeLampInTree(root->left)+closeLampInTree(root->right);
}
else{
struct TreeNode*a=root;
struct TreeNode*b=root;
struct TreeNode*c=root;
int x,y,z;
s(a);
rs(b);
fs(c);
x=1+closeLampInTree(a->left)+closeLampInTree(a->right);
y=1+closeLampInTree(b->left)+closeLampInTree(b->right);
z=1+closeLampInTree(c->left)+closeLampInTree(c->right);
return min(x,y,z);
}
}
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
//切换当前节点的灯的状态
void s(struct TreeNode* root){
if(root==NULL) return;
if(root->val==1) root->val=0;
else root->val=1;
}
//切换当前节点及其左右子节点的状态
void rs(struct TreeNode* root){
s(root);
s(root->left);
s(root->right);
}
//切换 以当前节点为根 的子树中,所有节点上的灯的状态
void fs(struct TreeNode* root){
if(root!=NULL){
s(root);
fs(root->left);
fs(root->right);
}
return;
}
int min(int a, int b, int c) {
if (a <= b && a <= c) {
return a;
} else if (b <= a && b <= c) {
return b;
} else {
return c; // 由于只有三个数,如果前两个条件都不满足,c自然是最小的
}
}
int closeLampInTree(struct TreeNode* root){
if(root==NULL) return 0;
if(root->val==0){
return closeLampInTree(root->left)+closeLampInTree(root->right);
}
else{
struct TreeNode*a=root;
struct TreeNode*b=root;
struct TreeNode*c=root;
int x,y,z;
s(a);
rs(b);
fs(c);
x=1+closeLampInTree(a->left)+closeLampInTree(a->right);
y=1+closeLampInTree(b->left)+closeLampInTree(b->right);
z=1+closeLampInTree(c->left)+closeLampInTree(c->right);
return min(x,y,z);
}
}