Re: [理工] binary tree
※ 引述《chenshengyua (阿丸)》之銘言:
: 各位大大
: 我想問一題頗重要的BT
: 題目是這樣
: Write a recursive program to check whether a given binary tree is symmetric with its structure(you don't need to check the values in the nodes)?
: 意思是用一個遞迴程式驗證二元樹是不是對稱!
: 小弟我有兩個想法
: 1.用兩個指標去驗左右子樹!
: 2.用level order traversal去算左右子樹的node數是否一樣!
: 但想歸想,這兩個想法我寫到一半都寫不下去!
: 希望高手可以有答案讓我參考參考!
: Wiki和古歌我都找不到!
: 我只剩這題這份就over了!
: 很需要這題呀!!拜託各位高手了!
Boolean symmetric(node*s,node*r)
{
bool ans=false;
if(s==NULL&&r==NULL) ans=true;//如果s跟r都是null表示樹相同
else if(s!=NULL&&r!=NULL)//如果都不是NUll的話
{
if(symmetric(s->data==r->data))//就檢查資料是否相同
{
if(symmetric(s->left,r->left)==true)//相同就開始檢查右子樹
ans=symmetric(s->right,r->right); //左子樹
}
}
return ans;//回傳答案
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.181.98.101
→
01/16 20:31, , 1F
01/16 20:31, 1F
→
01/16 20:31, , 2F
01/16 20:31, 2F
→
01/16 20:31, , 3F
01/16 20:31, 3F
→
01/16 21:06, , 4F
01/16 21:06, 4F
推
01/16 22:18, , 5F
01/16 22:18, 5F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):