?? 習題-44.c
字號:
//本程序只給出了算法思想
//讀者可以自己完善本程序
int found=FALSE;
Bitree* Find_Near_Ancient(Bitree T,Bitree p,Bitree q)
//求二叉樹T中結點p和q的最近共同祖先
{
Bitree pathp[ 100 ],pathq[ 100 ] //設立兩個輔助數組暫存從根到p,q的路徑
Findpath(T,p,pathp,0);
found=FALSE;
Findpath(T,q,pathq,0); //求從根到p,q的路徑放在pathp和pathq中
for(i=0;pathp[i]==pathq[i]&&pathp[i];i++)
; //查找兩條路徑上最后一個相同結點
return pathp[--i];
}//Find_Near_Ancient
void Findpath(Bitree T,Bitree p,Bitree path[ ],int i)//求從T到p路徑的遞歸算法
{
if(T==p)
{
found=TRUE;
return; //找到
}
path[i]=T; //當前結點存入路徑
if(T->lchild)
Findpath(T->lchild,p,path,i+1); //在左子樹中繼續尋找
if(T->rchild&&!found)
Findpath(T->rchild,p,path,i+1); //在右子樹中繼續尋找
if(!found)
path[i]=NULL; //回溯
}//Findpath
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -