<求助啊>三色二叉树问题
一棵二叉树可以按照如下规则表示成一个由0、1、2组成的字符序列,我们称之为“二叉树序列S”:例如,下图所表示的二叉树可以用二叉树序列S=21200110来表示。
任务是要对一棵二叉树的节点进行染色。每个节点可以被染成红色、绿色或蓝色。并且,一个节点与其子节点的颜色必须不同,如果该节点有两个子节点,那么这两个子节点的颜色也必须不相同。给定一棵二叉树的二叉树序列,请求出这棵树中最多和最少有多少个点能够被染成绿色
求大神给出下列程序的注释,(每一步都是干啥的)我真的一头雾水啊....感激不尽啊:::
#include <iostream>
#include <string>
#include <stdlib.h>
#define DISABLE -2
#define RED 1
#define GREEN 2
#define BLUE 3
#define NOCOLOR 0
typedef struct bitnode
{
int color;
struct bitnode * lchild,*rchild,*fathernode,*prenode;
}bitnode,*BITree;
BITree prenode=NULL;
using namespace std;
int curpos=0,sonstr=0;
int *toint;
int ball_color[3];
string convert;
void outputmaxmin()
{
int max=ball_color[0]>ball_color[1]?(ball_color[0]>ball_color[2]?ball_color[0]:ball_color[2]):(ball_color[1]>ball_color[2]?ball_color[1]:ball_color[2]);
cout<<max<<endl;
int min=ball_color[0]<ball_color[1]?(ball_color[0]<ball_color[2]?ball_color[0]:ball_color[2]):(ball_color[1]<ball_color[2]?ball_color[1]:ball_color[2]);
cout<<min<<endl;
}
void computecolornode(BITree temp_tree)
{
if(temp_tree)
{
ball_color[temp_tree->color-1]++;
computecolornode(temp_tree->lchild);
computecolornode(temp_tree->rchild);
}
}
void colornode(BITree &temp_tree)
{
if(temp_tree)
{
if(temp_tree->fathernode==NULL)
{
temp_tree->color=RED;
}
else if(temp_tree->fathernode->lchild==temp_tree||temp_tree->fathernode->lchild==NULL)
{
if(temp_tree->fathernode->color==RED)
temp_tree->color=GREEN;
if(temp_tree->fathernode->color==GREEN)
temp_tree->color=RED;
if(temp_tree->fathernode->color==BLUE)
temp_tree->color=RED;
}
else
{
if(temp_tree->fathernode->color==RED&&temp_tree->fathernode->lchild->color==GREEN)
temp_tree->color=BLUE;
if(temp_tree->fathernode->color==BLUE&&temp_tree->fathernode->lchild->color==RED)
temp_tree->color=GREEN;
if(temp_tree->fathernode->color==GREEN&&temp_tree->fathernode->lchild->color==RED)
temp_tree->color=BLUE;
}
colornode(temp_tree->lchild);
colornode(temp_tree->rchild);
}
}
string inputnode()
{
return convert.substr(sonstr++,1);
}
BITree xdd=NULL;
void constructbintree(BITree &temp_tree,string temp)
{
if(!("0"))
temp_tree=NULL;
else
{
temp_tree=(bitnode *)malloc(sizeof(bitnode));
temp_tree->color=NOCOLOR;
temp_tree->prenode=prenode;
xdd=prenode;
if(xdd==NULL)
temp_tree->fathernode=NULL;
else
{
while(xdd!=NULL)
{
if(xdd->lchild==temp_tree||xdd->rchild==temp_tree)
{
temp_tree->fathernode=xdd;
break;
}
xdd=xdd->prenode;
}
}
prenode=temp_tree;
constructbintree(temp_tree->lchild,inputnode());
constructbintree(temp_tree->rchild,inputnode());
}
return;
}
void makestring(int toint[],int length,string &convert)
{
convert="1";
int * temp=new int[length];
int cur=0,var=0;
while(cur<length)
{
if(toint[cur]>0)
{
convert+="1";
temp[cur]=toint[cur]-1;
}
if(toint[cur]==0)
{
convert+="00";
temp[cur]=DISABLE;
var=cur-1;
for(;var>=0;var--)
{
if(temp[var]!=DISABLE)
{
if(temp[var]==1)
{
convert+="1";
if(toint[var]==2)
{
temp[var]=DISABLE;
}
else
{
temp[var]--;
}
break;
}
else
{
convert+="0";
temp[var]=DISABLE;
}
}
}
}
cur++;
}
}
int main()
{
string seq;
BITree temp_tree;
cout<<"请输入二叉树序列"<<endl;
cin>>seq;
toint=new int[seq.length()];
for(int i=0;i<seq.length();i++)
{
toint[i]=atoi(seq.substr(i,1).c_str());
}
makestring(toint,seq.length(),convert);
constructbintree(temp_tree,inputnode());
colornode(temp_tree);
computecolornode(temp_tree);
outputmaxmin();
return 0;
}