SDUT2128树结构练习——排序二叉树的中序遍历

来源:互联网 发布:码云的独立域名 编辑:程序博客网 时间:2024/06/10 01:05
#include<bits/stdc++.h>using namespace std;struct tree{    int data;    struct tree *lc,*rc;};int k;void sorttree(struct tree *&node,int a){    if(node==NULL)    {        node=(struct tree *)malloc(sizeof(struct tree));        node->data=a;        node->lc=node->rc=NULL;    }    else if(a < node->data)        sorttree(node->lc,a);    else        sorttree(node->rc,a);}void mid(struct tree *node){    if(node)    {        mid(node->lc);        if(k==0)        {            printf("%d",node->data);            k++;        }        else printf(" %d",node->data);        mid(node->rc);    }}int main(){    int n;    while(~scanf("%d",&n))    {        k=0;        struct tree *root;        root=NULL;        while(n--)        {            int t;            scanf("%d",&t);            sorttree(root,t);        }        mid(root);        printf("\n");    }}

0 0
原创粉丝点击