用递归中序遍历二叉树 #include<stdlib.h> struct tree //声明树的结构 { struct tree *left; int data; struct tree *right; }; typedef struct tree treenode; type treenode *b_tree; //声明二叉树链表 //插入二叉树的节点 b_tree insert_node(b_tree root,int node) //其中root是原来二叉树的跟节点, { //node是将要插入的新节点的值 b_tree newnode; b_tree currentnode; b_tree parentnode; newnode=(b_tree)malloc(sizeof(treenode)); //建立新节点的内存空间 newnode->data=node; newnode->right=NULL; newnode->left=NULL; if(root=NULL) //表示原来的二叉树为空 return newnode; else //如果不为空的话 { currentnode=root; while(currentnode!=NULL) { parentnode=currentnode; if( currentnode->data>node) currentnode=currentnode->left; else currentnode=currentnode->right; } if(parentnode->data>node) parentnode->left=newnode; else parentnode->right=newnode; } return root; } // 建立二叉树 b_tree create_btree(int *data,int len) { b_tree root=NULL; int i; for(i=0;i<len;i++) root=insert_node(root,data[i]); return root; } //二叉树中序遍历 void inorder(b_tree point) { if(point!=NULL) { inorder(point->left); printf("%d",point->data); //中序遍历是先左孩子,然后是根节点,最后是有孩子节点 inorder(point->right); } } //主程序 void main( ) { b_tree root=NULL; int i,index; int value; int nodelist[20]; printf("/n pleaase input the elements of binary tree(exit for 0 ):/n"); index=0; //读取数值存到数组中 scanf("%d",&value); while(value!=0) { nodelist[index]=value]; index=index+1; scanf("%d",&value); } //建立二叉树 root=create_btree(nodelist,index); //中序遍历二叉树 printf("/nThe inorder traversal result is :"); inorder(root); printf("/n"); }