要求用C評語言來寫并能在TC2。0中可以運(yùn)行的才行數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)四兩棵二叉樹稱作相似的,它們要么全為空。要么不是空樹但它們的左子樹相似且右子樹相似。請?jiān)O(shè)計(jì)一個程序判斷兩棵二叉樹是否相似。
熱心網(wǎng)友
你自己試試吧,應(yīng)該不會有什么大問題不相似時還能計(jì)算出有幾處#include "stdio。h"#include "stdlib。h"int N=0;typedef struct node{int value;node *lchild,*rchild;}NODE,*Node;void judge(Node firstRoot,Node secondRoot){if(firstRoot-lchild!=NULL && secondRoot-lchild!=NULL)judge(firstRoot-lchild,secondRoot-lchild);//用左子樹當(dāng)樹根遞歸調(diào)用if(firstRoot-rchild!=NULL && secondRoot-rchild!=NULL)judge(firstRoot-rchild,secondRoot-rchild);if((firstRoot-lchild!=secondRoot-lchild)&&(firstRoot-lchild==NULL||second-lchild==NULL))N++;if((firstRoot-rchild!=secondRoot-rchild)&&(firstRoot-rchild==NULL||second-rchild==NULL))N++;}void main(){Node firstTree,secondTree;//假設(shè)都已經(jīng)初始化if(firstTree==NULL && secondTree==NULL){printf("這兩棵樹相似,且根都為空\n");return;}if((firstTree!=secondTree&&(firstTree==NULL||secondTree==NULL)){printf("這兩棵樹不相似,且其中一棵樹根為空\n");return;}judge(firstTree,secondTree);if(N==0)printf("這兩棵樹完全相似,且樹根都不為空\n");return;}elseprintf("這兩棵樹不相似,且樹根都不為空,共有%d處不相似\n",N)return;}}注意:這兩棵樹的樹葉的左子樹和右子樹都必須指向空,否則可能會出錯我在是VC 6。0下寫的 自己調(diào)試調(diào)試吧 我不保證可以。