SDUT2128樹結構練習——排序二叉樹的中序遍歷
阿新 • • 發佈:2019-01-22
#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"); } }