国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

如何判斷兩顆二叉樹(shù)同構(gòu)

2019-11-08 20:31:12
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

#define MaxTree 10#define ElementType char#define Tree int#define Null -1struct TreeNode{ElementType Element; Tree Left;Tree Right; }T1[MaxTree],T2[MaxTree],T[MaxTree];int main(){Tree R1,R2;R1=BuildTree(T1);R2=BuildTree(T2);if(Isomorphic(R1,R2)) PRintf("Yes/n");else printf("No/n"); return 0;} Tree BuildTree(struct TreeNode T[])//如何建二叉樹(shù) { if(N){for(i=0;i<N;i++) check[i]=0;for(i=0;i<N;i++){scanf("%c %c %c/n",&T[i].Element,&cl,&cr);if(cl!='-'){T[i].Left=cl-'0';check[T[i].Left]=1;}else T[i].Left=Null;if(cr!='-'){T[i].Right=cr-'0';check[T[i].Right]=1;}else T[i].Right=Null;//對(duì)cr的對(duì)應(yīng)處理 }for(i=0;i<N;i++)if(!check[i]) break;Root=i;}return Root;}int Isomorphic(Tree R1,Tree R2)//如何判別兩二叉樹(shù)同構(gòu) {if((R1==Null)&&(R2==Null))//both emptyreturn 1;if((R1==Null)&&(R2==Null)||((R1!=Null)&&(R2!=Null)))return 0;//one of them is emptyif(T1[R1].Element!=T2[R2].Element)return 0;//roots are differentif((T1[R1].Left==Null)&&(T2[R2].Left==Null))//both have no left subtreereturn Isomorphic(T1[R1].Right,T2[R2].Right);if(((T1[R1].Left!=Null)&&(T2[R2].Left!=Null))&&  ((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)))  //no need to swap the left and the right  return(Isomorphic(T1[R1].Left,T2[R2].Left)&&    Isomorphic(T1[R1].Right,T2[R2].Right));else//need to swap the left and the rightreturn(Isomorphic(T1[R1].Left,T2[R2].Right)&&   Isomorphic(T1[R1].Right,T2[R2].Left));} 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 当雄县| 阜平县| 石狮市| 安新县| 禹州市| 深水埗区| 黑龙江省| 科技| 四平市| 呼伦贝尔市| 汉阴县| 秦安县| 武川县| 松桃| 天等县| 平昌县| 洛宁县| 张掖市| 长宁县| 三明市| 福泉市| 乌恰县| 张掖市| 聂拉木县| 义乌市| 东平县| 同江市| 昌吉市| 益阳市| 上饶县| 遵化市| 十堰市| 镇平县| 吉木乃县| 元阳县| 阿鲁科尔沁旗| 阳朔县| 武冈市| 凤凰县| 卫辉市| 普洱|