HDU-3999 The order of a Tree 树的遍历

来源:互联网 发布:淘宝买快排被查 编辑:程序博客网 时间:2024/05/23 01:57

题目链接

对指针的理解运用还欠加强

#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<math.h>#include<functional>#include<algorithm>#include<vector>#include<queue>using namespace std;const int maxn =100005;const int inf = 1<<30;//0x7f;typedef __int64 LL;int n;struct node{int w;node *lch,*rch;};node *updata( int u,node *root ){if( root == NULL ){root = new node;root->w = u; root->lch = root->rch = NULL;return root;}else if( root->w > u )root->lch = updata( u,root->lch );else if( root->w < u )root->rch = updata( u,root->rch );return root;}void fun( node *root,int flag ){if( flag )printf("%d",root->w);elseprintf(" %d",root->w);if( root->lch != NULL  )fun( root->lch,0 );if( root->rch != NULL )fun( root->rch,0 );}int main(){#ifndef ONLINE_JUDGE    freopen("data.txt","r",stdin);#endifint u;while( scanf("%d",&n) != EOF ){node *root = NULL;for( int i = 1; i <= n; i ++ ){scanf("%d",&u);root = updata(u,root);}fun( root,1 );puts("");}    return 0;}


0 0
原创粉丝点击