/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct tree {
int val;
struct tree *left;
struct tree *right;
struct tree *nei;
};
struct tree* createNode(int val) {
struct tree* newNode = (struct tree*)malloc(sizeof(struct tree));
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
newNode->nei=NULL;
return newNode;
}
struct tree* copyTree(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct tree* newNode = createNode(root->val);
newNode->left = copyTree(root->left);
newNode->right = copyTree(root->right);
return newNode;
}
struct tree* fcopytree(struct tree* root) {
if (root == NULL)
return NULL;
struct tree* a = NULL; // 提前声明
if (root->val == 1) {
a = createNode(0);
} else {
a = createNode(1);
}
a->nei = root;
root->nei = a;
a->left = fcopytree(root->left);
a->right = fcopytree(root->right);
return a;
}
void s(struct tree* root){
if(root==NULL) return;
if(root->val==1) root->val=0;
else root->val=1;
}
void rs(struct tree* root){
s(root);
s(root->left);
s(root->right);
}
void fs(struct tree* root){
if(root==NULL) return;
s(root);
fs(root->left);
fs(root->right);
}
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 f(struct tree*root){
if(root==NULL) return 0;
if(root->val==0){
return f(root->left)+f(root->right);
}
else{
int x,y,z;
s(root);
x=1+f(root->left)+f(root->right);
s(root);
rs(root);
y=1+f(root->left)+f(root->right);
rs(root);
z=1+f(root->nei->left)+f(root->nei->right);
return min(x,y,z);
}
}
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct tree {
int val;
struct tree *left;
struct tree *right;
struct tree *nei;
};
struct tree* createNode(int val) {
struct tree* newNode = (struct tree*)malloc(sizeof(struct tree));
newNode->val = val;
newNode->left = NULL;
newNode->right = NULL;
newNode->nei=NULL;
return newNode;
}
struct tree* copyTree(struct TreeNode* root) {
if (root == NULL) {
return NULL;
}
struct tree* newNode = createNode(root->val);
newNode->left = copyTree(root->left);
newNode->right = copyTree(root->right);
return newNode;
}
struct tree* fcopytree(struct tree* root) {
if (root == NULL)
return NULL;
struct tree* a = NULL; // 提前声明
if (root->val == 1) {
a = createNode(0);
} else {
a = createNode(1);
}
a->nei = root;
root->nei = a;
a->left = fcopytree(root->left);
a->right = fcopytree(root->right);
return a;
}
void s(struct tree* root){
if(root==NULL) return;
if(root->val==1) root->val=0;
else root->val=1;
}
void rs(struct tree* root){
s(root);
s(root->left);
s(root->right);
}
void fs(struct tree* root){
if(root==NULL) return;
s(root);
fs(root->left);
fs(root->right);
}
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 f(struct tree*root){
if(root==NULL) return 0;
if(root->val==0){
return f(root->left)+f(root->right);
}
else{
int x,y,z;
s(root);
x=1+f(root->left)+f(root->right);
s(root);
rs(root);
y=1+f(root->left)+f(root->right);
rs(root);
z=1+f(root->nei->left)+f(root->nei->right);
return min(x,y,z);
}
}