多效第16场 HDU 3999 The order of a Tree(二叉树)
2011-09-01 21:09
495 查看
没学过二叉树的先序遍历
#include <cstdio> #include <string.h> const int maxn=100050; struct Node{ int l,r,w; }node[maxn]; int m; int cnt; void insert (int n,int root) { if(node[root].w<n)//右子树 { if(~node[root].r) { insert(n,node[root].r); } else { node[root].r=cnt; node[cnt++].w=n; } } else { if(~node[root].l) { insert(n,node[root].l); } else { node[root].l=cnt; node[cnt++].w=n; } } } void preorder(int n) { printf(" %d",node .w); if(~node .l) preorder(node .l); if(~node .r) preorder(node .r); } inline void init () { memset (node , -1 , sizeof(node)); cnt=0; } int main () { int w; while (~scanf("%d",&m)) { init(); scanf("%d",&w); node[cnt++].w=w; for (int i=1 ; i<m ; ++i) { scanf("%d",&w); insert (w,0); } printf("%d",node[0].w); if(~node[0].l) preorder(node[0].l); if(~node[0].r) preorder(node[0].r); printf("\n"); } return 0; }
相关文章推荐
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- HDU 3999----The order of a Tree(二叉树的前序遍历)
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- HDU(3999)The order of a Tree
- HDU 3999 The order of a Tree
- hdu 3999 The order of a Tree
- 二叉搜索树的前序遍历:hdu 3999 The order of a Tree
- Hdu - 3999- The order of a Tree
- HDU3999 The order of a Tree(构建二叉搜索树模板指针)
- hdu 3999 The order of a Tree (二叉搜索树)
- hdu 3999 The order of a Tree
- HDOJ 题目3999 The order of a Tree(二叉树)
- hdu 3999 The order of a Tree
- HDU 3999 The order of a Tree 二叉搜索树 BST
- hdu 3999 The order of a Tree
- HDU 3999 The order of a Tree 二叉搜索树 BST
- HDU 3999 The order of a Tree
- hdu 3999 The order of a Tree(二叉搜索树)
- HDU 3999 The order of a Tree (二叉搜索树+先序遍历)