标签:create struct typedef lib def main for nod tree
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *left, *right; }Node; Node * CreateTree(int n) { int a[101],i; for(i=0;i<n;i++) scanf("%d",&a[i]); Node* t1; t1=(Node*)malloc(sizeof(Node)); t1->data=a[0]; t1->left=t1->right=NULL; for(i=1;i<n;i++) { Node* t2,*p=t1,*q; t2=(Node*)malloc(sizeof(Node)); t2->data=a[i]; t2->left=t2->right=NULL; while(p) { if(p->data>a[i]) { q=p; p=p->left; } else { q=p; p=p->right; } } if(q->data>a[i]) q->left=t2; else q->right=t2; } return t1; } void Preorder(Node *tree) { if(tree==NULL) return ; Preorder(tree->left); printf("%d ",tree->data); Preorder(tree->right); } int main() { Node *tree; int n; while(~scanf("%d",&n)) { tree=CreateTree(n); Preorder(tree); printf("\n"); } }
标签:create struct typedef lib def main for nod tree
原文地址:https://www.cnblogs.com/zzjam--1/p/11336961.html