hdu 3791 二叉搜尋樹

2021-07-16 13:07:32 字數 1084 閱讀 6389

problem description

判斷兩序列是否為同一二叉搜尋樹序列

input

開始乙個數n,(1<=n<=20) 表示有n個需要判斷,n= 0 的時候輸入結束。

接下去一行是乙個序列,序列長度小於10,包含(0~9)的數字,沒有重複數字,根據這個序列可以構造出一顆二叉搜尋樹。

接下去的n行有n個序列,每個序列格式跟第乙個序列一樣,請判斷這兩個序列是否能組成同一顆二叉搜尋樹。

output

如果序列相同則輸出yes,否則輸出no

sample input

2

567432

543267

5763420

sample output

yes

no

ps:分別建立兩棵二叉搜尋樹,對每個節點進行比較即可

#include #include#include#includeusing namespace std;

struct node

;struct node *creat(struct node *root,int k)

else if(k>root->data)

else

return root;

}int f=0;

void comp(struct node *root,struct node *head)

else

}else if(root&&!head)

else if(!root&&head)

return ;

}int main()

{ int n;

char a[15],b[15];

while(cin>>n,n)

{cin>>a;

struct node *root;

root=new node;

root=null;

int len=strlen(a);

for(int i=0; i>b;

int lenb=strlen(b);

for(int i=0; i

hdu3791二叉搜尋樹

hdu3791二叉搜尋樹 又是二叉搜尋樹的前序遍歷。1.建樹會順序影響整棵樹的形狀 2.記得釋放資源 3.可以用雙重指標和引用優化程式。某些printf 是之前設定的斷點,可以無視之 includeusing namespace std struct bst root bst insert bst ...

HDU 3791 二叉搜尋樹

problem description 判斷兩序列是否為同一二叉搜尋樹序列 input 開始乙個數n,1 n 20 表示有n個需要判斷,n 0 的時候輸入結束。接下去一行是乙個序列,序列長度小於10,包含 0 9 的數字,沒有重複數字,根據這個序列可以構造出一顆二叉搜尋樹。接下去的n行有n個序列,每...

hdu 3791 二叉搜尋樹

做這道題首先知道二叉樹搜尋樹的定義是關鍵 注意 二叉樹和二叉搜尋樹 bst 的概念是不同的,二叉搜尋樹是一種特殊的二叉樹。它符合規律 一開始沒有搞明白兩者的區別,以為二叉搜尋樹就是二叉樹,這讓我在構造二叉樹的時候產生了疑惑,因為根據資料結構書上的描述,二叉樹可以根據前序 中序 後序序列構造,這樣產生...