标签:lse void == class std span while typedef main
#include<stdio.h> #include<stdlib.h>//中序建立二叉树 typedef struct node { struct node *left,*right; int data; int rTag,lTag; }BNode; BNode* pre=NULL;//pre总是指向其前一个结点 BNode* CreateBTree() { int data, front=1, rear = 0; BNode *t, *root, *q[1001]; while(scanf("%d", &data), data!=-1) { if(data==0) t = NULL; else { t = (BNode*)malloc(sizeof(BNode)); t->data = data; t->left = NULL; t->right = NULL; } q[++rear] = t; if(rear==1) root= t; else { if(q[front]!=NULL) { if(rear%2==0) q[front]->left = t; else q[front]->right = t; } if(rear%2==1) front++; } } return root; } void InThreading(BNode*p)//如果某个结点的左孩子为空,则将空的左孩子指针域改为指向其前驱 { if(p)//如果某结点的右孩子为空,则将空的右孩子指针域改为指向其后继 { InThreading(p->left); if(!p->left) { p->lTag = 1; p->left = pre;//左孩子指向前驱 } if(pre && !pre->right) { pre->rTag = 1; pre->right = p;//右孩子指向后继 } pre=p;//记住上一个结点 InThreading(p->right); } } void InOrderThread(BNode* p) { while(p) { while(p->lTag==0) p = p->left; printf(" %d", p->data); while(p->rTag==1 && p->right!=NULL) { p = p->right; printf(" %d", p->data); } p = p->right; } } int main() { BNode *root; root=CreateBTree(); InThreading(root); InOrderThread(root); return 0; }
标签:lse void == class std span while typedef main
原文地址:https://www.cnblogs.com/AQhhhh/p/10891015.html