hdu 3999(二叉排序树)
来源:互联网 发布:sql的insert语句 编辑:程序博客网 时间:2024/06/06 12:44
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999
思路:创建一颗二叉排序树,直接先序遍历即可。
View Code
1 #include<iostream> 2 using namespace std; 3 bool first; 4 5 struct BST{ 6 int data; 7 BST *leftchild; 8 BST *rightchild; 9 };10 11 void Build(BST *&root,int x){12 if(root==NULL){13 root=(BST *)malloc(sizeof(BST));14 root->data=x;15 root->leftchild=root->rightchild=NULL;16 }else {17 if(x<root->data){18 Build(root->leftchild,x);19 }else 20 Build(root->rightchild,x);21 }22 }23 24 void Search(BST *root){25 if(root!=NULL&&first){26 first=false;27 printf("%d",root->data);28 Search(root->leftchild);29 Search(root->rightchild);30 }else if(root!=NULL){31 printf(" %d",root->data);32 Search(root->leftchild);33 Search(root->rightchild);34 }35 }36 37 38 int main(){39 int n;40 while(~scanf("%d",&n)){41 BST *root=NULL;42 first=true;43 for(int i=1;i<=n;i++){44 int x;45 scanf("%d",&x);46 Build(root,x);47 }48 Search(root);49 printf("\n");50 }51 return 0;52 }
PS:也可以用线段树建树的。。。
0 0
- hdu 3999(二叉排序树)
- 二叉排序树——HDU 3791
- HDU 5444 Elven Postman 二叉排序树
- (二叉排序树)HDU 3791 二叉搜索树
- HDU 4125 Moles 二叉排序树 树状数组 kmp
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- 二叉排序树
- hdu 1528+hdu 1962(最小覆盖)
- hdu 1420(快速幂)
- hdu 1075(字典树)
- hdu 1247(Trie)
- hdu 1426(九宫格)
- hdu 3999(二叉排序树)
- hdu 3732(01背包转多重背包)
- hdu 3709+hdu 3555(数位dp)
- hdu 1224(最长路)
- hdu 2452+hdu 1501
- hdu 4540(简单dp)
- hdu 1078+hdu1978+hdu 1428
- hdu 1269+hdu 2767(强连通分量)
- hdu 2208(dfs)