二叉搜尋樹
判斷兩序列是否為同一二叉搜尋樹序列
開始乙個數n,(1<=n<=20) 表示有n個需要判斷,n= 0 的時候輸入結束。接下去一行是乙個序列,序列長度小於10,包含(0~9)的數字,沒有重複數字,根據這個序列可以構造出一顆二叉搜尋樹。
接下去的n行有n個序列,每個序列格式跟第乙個序列一樣,請判斷這兩個序列是否能組成同一顆二叉搜尋樹。
如果序列相同則輸出yes,否則輸出no
2567432
543267
576342
0
yesno
#include#include#include#include#includeusing namespace std;
struct node;
typedef struct node* bit;
bit inserttree(bit t,int ch)
else if(ch>t->data)t->rchild=inserttree(t->rchild,ch);
else t->lchild=inserttree(t->lchild,ch);
return t;
} int flag=0;
void check(bit at,bit bt)
return ;
}int main()
while(n--)
bit bt=null;
for(int i=0;icheck(at,bt);
if(flag==1)
else
} }
return 0;
}
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 二叉搜尋樹
problem description 判斷兩序列是否為同一二叉搜尋樹序列 input 開始乙個數n,1 n 20 表示有n個需要判斷,n 0 的時候輸入結束。接下去一行是乙個序列,序列長度小於10,包含 0 9 的數字,沒有重複數字,根據這個序列可以構造出一顆二叉搜尋樹。接下去的n行有n個序列,每...