hdu 3999(二叉排序树)
2013-04-06 11:38
295 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999
思路:创建一颗二叉排序树,直接先序遍历即可。
View Code
PS:也可以用线段树建树的。。。
思路:创建一颗二叉排序树,直接先序遍历即可。
View Code
#include<iostream> using namespace std; bool first; struct BST{ int data; BST *leftchild; BST *rightchild; }; void Build(BST *&root,int x){ if(root==NULL){ root=(BST *)malloc(sizeof(BST)); root->data=x; root->leftchild=root->rightchild=NULL; }else { if(x<root->data){ Build(root->leftchild,x); }else Build(root->rightchild,x); } } void Search(BST *root){ if(root!=NULL&&first){ first=false; printf("%d",root->data); Search(root->leftchild); Search(root->rightchild); }else if(root!=NULL){ printf(" %d",root->data); Search(root->leftchild); Search(root->rightchild); } } int main(){ int n; while(~scanf("%d",&n)){ BST *root=NULL; first=true; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); Build(root,x); } Search(root); printf("\n"); } return 0; }
PS:也可以用线段树建树的。。。
相关文章推荐
- HDU_3999_二叉排序树
- hdu 3999(二叉排序树)
- HDU 3999 二叉排序树
- hdu 3999 二叉排序树
- hdu 3999:The order of a Tree
- hdu 3791 二叉排序树
- HDU 4125 Moles 二叉排序树 树状数组 kmp
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- (二叉排序树)HDU 3791 二叉搜索树
- HDU 3999 (13.07.07)
- 二叉搜索树的前序遍历:hdu 3999 The order of a Tree
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- HDU - 5444 Elven Postman 二叉排序树
- hdu 3999 The order of a Tree
- 二叉树——HDU 3999
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree (二叉搜索树)
- hdu 3359 /poj 3999 Kind of a Blur(高斯消元模板题)
- hdu 3999 The order of a Tree
- HDU 3999 The order of a Tree 二叉搜索树 BST