前言:

  排版很难看,没办法,我绝对不是因为懒得排而懒得排,而是因为只有被命运石之门选中的人才能从头到尾够看到底。

先序创建二叉树

(这里用了C++  <引用>的特性,使用二重指针代替或者将函数返回值设成指针再做点小修改也能实现)

 

 1 void CreateTree(TreeRoot &Root)
 2 {//创建(先序)
 3     char c;
 4     c=getchar();
 5     if(c!='#')
 6     {
 7         Root=(TreeRoot)malloc(sizeof(TNode));
 8         Root->data=c;
 9         CreateTree(Root->pleft);
10         CreateTree(Root->pright);
11     }
12     else
13     {
14         Root=NULL;
15     }
16 }

 

 

二叉树遍历(前|中|后 序)--递归(核心代码)

 1  
 2 void InorderTraversal( BinTree BT )
 3 {
 4     if( BT ) {
 5         InorderTraversal( BT->Left );
 6         /* 此处假设对BT结点的访问就是打印数据 */
 7         printf("%d ", BT->Data); /* 假设数据为整型 */
 8         InorderTraversal( BT->Right );
 9     }
10 }
11  
12 void PreorderTraversal( BinTree BT )
13 {
14     if( BT ) {
15         printf("%d ", BT->Data );
16         PreorderTraversal( BT->Left );
17         PreorderTraversal( BT->Right );
18     }
19 }
20  
21 void PostorderTraversal( BinTree BT )
22 {
23     if( BT ) {
24         PostorderTraversal( BT->Left );
25         PostorderTraversal( BT->Right );
26         printf("%d ", BT->Data);
27     }
28 }
29 
30 ————————————————
31 版权声明:本文为CSDN博主「BrianOne」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
32 原文链接:https://blog.csdn.net/Brianone/article/details/89440024

二叉树遍历(前|中|后|层 序)--非递归(核心代码)

 1 void PreOrderFDG(TreeRoot Root)
 2 {//先序遍历非递归
 3     SqStack S;
 4     InitStack(S);
 5     while(Root!=NULL || Empty(S)==0)
 6     {
 7         if(Root!=NULL)
 8         {
 9             printf("%c ",Root->data);
10             Push(S,Root);
11             Root=Root->pleft;
12         }
13         else
14         {
15             Pop(S,Root);
16             Root=Root->pright;
17         }
18     }
19 }
 1 void InOrderFDG(TreeRoot Root)
 2 {//中序遍历非递归
 3     SqStack S;
 4     InitStack(S);
 5     while(Root!=NULL || Empty(S)==0)
 6     {
 7         if(Root!=NULL)
 8         {
 9             Push(S,Root);
10             Root=Root->pleft;
11         }
12         else
13         {
14             Pop(S,Root);
15             printf("%c ",Root->data);
16             Root=Root->pright;
17         }
18     }
19 }
 1 //后序遍历非递归算法
 2 typedef struct SNode{
 3     BiTree p;
 4     int tag;
 5 }SNode;
 6 //后序遍历使用的进栈函数
 7 void postpush(SNode *a,SNode sdata){
 8     a[++top]=sdata;
 9 }
10 //后序遍历函数
11 void PostOrderTraverse(BiTree Tree){
12     SNode a[20];//定义一个顺序栈
13     BiTNode * p;//临时指针
14     int tag;
15     SNode sdata;
16     p=Tree;
17     while (p||top!=-1) {
18         while (p) {
19             //为该结点入栈做准备
20             sdata.p=p;
21             sdata.tag=0;//由于遍历是左孩子,设置标志位为0
22             postpush(a, sdata);//压栈
23             p=p->lchild;//以该结点为根结点,遍历左孩子
24         }
25         sdata=a[top];//取栈顶元素
26         pop();//栈顶元素弹栈
27         p=sdata.p;
28         tag=sdata.tag;
29         //如果tag==0,说明该结点还没有遍历它的右孩子
30         if (tag==0) {
31             sdata.p=p;
32             sdata.tag=1;
33             postpush(a, sdata);//更改该结点的标志位,重新压栈
34             p=p->rchild;//以该结点的右孩子为根结点,重复循环
35         }
36         //如果取出来的栈顶元素的tag==1,说明此结点左右子树都遍历完了,可以调用操作函数了
37         else{
38             displayElem(p);
39             p=NULL;
40         }
41     }
42 }

后序 双栈法

 1 void NotRecursionPostOrder(BTree T){
 2   PLinkStack S,CS;
 3   S=Init_Stack();
 4   CS=Init_Stack();
 5   while(T || !empty_Stack(S)){
 6     if(T){
 7       Push_Stack(S,T);
 8       Push_Stack(CS,T);
 9       T=T->right;
10     }else{
11       T=Pop_Stack(S)->data;
12       T=T->left;
13     }
14   }
15   while(CS->top!=NULL){
16     printf("%c",CS->top->data->element);
17     CS->top=CS->top->next;
18   }
19   DestroyStack(CS);
20 }

 

 1 void LevelorderTraversal ( BinTree BT )//层序遍历
 2 { 
 3     Queue Q; 
 4     BinTree T;
 5  
 6     if ( !BT ) return; /* 若是空树则直接返回 */
 7      
 8     Q = CreatQueue(); /* 创建空队列Q */
 9     AddQ( Q, BT );
10     while ( !IsEmpty(Q) ) {
11         T = DeleteQ( Q );
12         printf("%d ", T->Data); /* 访问取出队列的结点 */
13         if ( T->Left )   AddQ( Q, T->Left );
14         if ( T->Right )  AddQ( Q, T->Right );
15     }
16 
17 ————————————————
18 版权声明:本文为CSDN博主「BrianOne」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
19 原文链接:https://blog.csdn.net/Brianone/article/details/89440024

 

点这里有个C++ 版,方法很多,只会C的话应该能看懂思路

点这里有思路清晰的C语言版本

点这里有个后续遍历的不错思路——节点里增加了一个变量记录次数(或者用哈希表也可以)

后序遍历方法挺多,多搜一搜,特别是C++ 代码最好也能看看,有很多优秀的实现都是c++版的。

求二叉树高度

 1 int PostTreeDepth(TreeRoot Root)
 2 {//求二叉树的高度
 3     int hl,hr,max;
 4     if(Root!=NULL)
 5     {
 6         hl=PostTreeDepth(Root->pleft);
 7         hr=PostTreeDepth(Root->pright);
 8         max=hl>hr?hl:hr;
 9         return max+1;
10     }
11     else
12         return 0;
13 }

按树状打印二叉树

 1 void PrintTree(TreeRoot Root,int nLayer)
 2 {//按树状打印二叉树
 3     int i;
 4     if(Root==NULL)
 5         return ;
 6     PrintTree(Root->pright,nLayer+1);
 7     for(i=0;i<=nLayer;i++)
 8     {
 9         printf("  ");
10     }
11     printf("%c\n",Root->data);
12     PrintTree(Root->pleft,nLayer+1);
13 }

输出二叉树叶子节点并统计叶子节点的数目

 1 void PreOrderLeaf(TreeRoot Root)
 2 {//输出二叉树的叶子结点,并统计叶子结点的数目
 3     if(Root!=NULL)
 4     {
 5         if(Root->pleft==NULL && Root->pright==NULL)
 6         {
 7             printf("%c ",Root->data);
 8             leafcount++;
 9         }
10         PreOrderLeaf(Root->pleft);
11         PreOrderLeaf(Root->pright);
12     }
13 }

 

哈夫曼树(编码)

 

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 typedef struct hfNode {
  5     int weight;
  6     struct hfNode* lch, * rch, * pnt;
  7 }hfNode;
  8 
  9 void selMin(hfNode* nodes, int n, int* a, int* b) { //求两个最小值的下标
 10     int temp[3] = { 0x3f3f3f3f,0x3f3f3f3f,0x3f3f3f3f };
 11     int i;
 12     for (i = 0; i < n; ++i) {
 13         //printf("%d,%d;", temp[1], temp[2]);
 14         if (nodes[i].pnt == 0) {
 15             int t = nodes[i].weight;
 16             if (temp[1] == 0x3f3f3f3f) {
 17                 temp[1] = t;
 18                 *a = i;
 19             }
 20             else if (temp[2] == 0x3f3f3f3f) {
 21                 temp[2] = t;
 22                 *b = i;
 23             }
 24             else if (temp[1] > t) {
 25                 temp[1] = t;
 26                 *a = i;
 27             }
 28             else if (temp[2] > t) {
 29                 temp[2] = t;
 30                 *b = i;
 31             }
 32         }
 33         //printf("%d,%d;\n", temp[1], temp[2]);
 34     }
 35 }
 36 
 37 void CreateTree(hfNode* nodes, char** hfCode, int* w, int n) {  //构造 Huffman Tree   规定向左走则生成0  右 1
 38     int m = 2 * n - 1;
 39     int j;
 40     int len;
 41     char* helper = (char*)malloc(2 * n * sizeof(char));
 42 
 43     for (int i = n; i < m; ++i) {
 44         int a, b;
 45         selMin(nodes, m, &a, &b);
 46         nodes[i].pnt = 0;
 47         nodes[i].lch = &nodes[a];
 48         nodes[i].rch = &nodes[b];
 49         nodes[a].pnt = &nodes[i];
 50         nodes[b].pnt = &nodes[i];
 51         nodes[i].weight = nodes[a].weight + nodes[b].weight;
 52     }
 53     //树建立完毕 下面开始生成哈夫曼码
 54     for (int i = 0; i < n; ++i) {
 55         hfNode* cur = nodes + i;
 56         j = 0;
 57         while (cur->pnt != 0) {
 58             hfNode* p = cur->pnt;
 59             if (p->lch == cur) {
 60                 helper[j++] = '0';
 61             }
 62             else {
 63                 helper[j++] = '1';
 64             }
 65             cur = cur->pnt;
 66         }
 67         hfCode[i] = (char*)malloc(sizeof(char) * (j + 1));
 68         len = 0;
 69         while (j > 0) {
 70             hfCode[i][len++] = helper[--j];
 71         }
 72         hfCode[i][len] = '\0';
 73     }
 74     free(helper);
 75 }
 76 void show(char** hfCode, int n) {
 77     //int n;
 78     int i;
 79     printf("\n按照输入顺序   哈夫曼编码依次为:\n");
 80     //printf("%d,%d\n", sizeof(hfCode) , sizeof(char*));
 81     //n = sizeof(hfCode) / sizeof(char*);
 82     for (i = 0; i < n; ++i) {
 83         printf("%s\n", hfCode[i]);
 84     }
 85     printf("\n================================\n");
 86 }
 87 void main() {
 88     hfNode* nodes;
 89     char** hfCode;
 90     int n;
 91     int* w;
 92     int i;
 93     printf("请输入字符数量:\n");
 94     scanf("%d", &n);
 95     w = (int*)malloc(n * sizeof(char*));
 96     hfCode = (char**)malloc(n * sizeof(char*));
 97     nodes = (hfNode*)malloc((2 * n - 1) * sizeof(hfNode));
 98     printf("请依次输入字符权重:\n");
 99     for (i = 0; i < n; ++i) {
100         scanf("%d", w + i);
101         (nodes + i)->weight = w[i];
102         (nodes + i)->lch = (nodes + i)->rch = (nodes + i)->pnt = 0;
103     }
104     CreateTree(nodes, hfCode, w, n);
105     show(hfCode, n);
106     printf("%p,%p", (nodes + 1)->pnt, (nodes + 3)->pnt);
107     free(w);
108     free(nodes);
109     free(hfCode);
110 
111 }
112 /*
113 A  B  C  D
114 3  1  2  1
115 应该的编码之一应该为:
116 
117 0
118 101
119 11
120 100
121 */

 

二叉搜索树--递归--插入,删除,和查找

  1 //二叉查找树   递归实现  插入,删除和查找
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 
  5 typedef struct Node {
  6     int val;
  7     struct Node* left;
  8     struct Node* right;
  9 }Node;
 10 typedef int bool;
 11 
 12 Node * Find(Node* root,int a);   //如果找到了返回指针 如果找不到则返回空指针
 13 Node *Insert(Node* root, int a);  //将a插入到root中并且返回根节点指针
 14 Node *Delete(Node* root,int a);   //将a从root中删除并且返回根节点指针    
 15 Node* FindMin(Node* root);//找到最小节点并且返回该节点的指针
 16 Node* FindMax(Node* root);//找到最大节点并且返回该节点的指针
 17 void PreorderTraversal(Node* root); //先序遍历
 18 int main(){
 19     Node* root = (Node*)malloc(sizeof(Node));//根节点
 20     Node * BST, *MinP, *MaxP, *Tmp;   //分别是树根节点   最小节点 最大节点 和一个临时用的节点指针变量
 21     int X;
 22     int N, i;
 23     BST = NULL;
 24     scanf("%d", &N);//插入节点
 25     for (i = 0; i < N; i++) {
 26         scanf("%d", &X);
 27         BST = Insert(BST, X);
 28     }
 29     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍历
 30     MinP = FindMin(BST);
 31     MaxP = FindMax(BST);
 32     scanf("%d", &N);//查找节点
 33     for (i = 0; i < N; i++) {
 34         scanf("%d", &X);
 35         Tmp = Find(BST, X);
 36         if (Tmp == NULL) printf("%d is not found\n", X);
 37         else {
 38             printf("%d is found\n", Tmp->val);
 39             if (Tmp == MinP) printf("%d is the smallest key\n", Tmp->val);
 40             if (Tmp == MaxP) printf("%d is the largest key\n", Tmp->val);
 41         }
 42     }
 43     scanf("%d", &N);//删除节点
 44     for (i = 0; i < N; i++) {
 45         scanf("%d", &X);
 46         BST = Delete(BST, X);
 47     }
 48     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍历
 49     return 0;
 50 }
 51 
 52 Node *Find(Node* root,int a) { //查找
 53     if (!root) return NULL;
 54     if (a == root->val) {
 55         return root;
 56     }
 57     else if (a > root->val) {
 58         return Find(root->right, a);
 59     }
 60     else {//a< root->val
 61         return Find(root->left, a);
 62     }
 63     return NULL;//实际上这一行是没有必要的
 64 }
 65 Node *Insert(Node* root, int a) {
 66     if (!root) {
 67         Node* newNode = (Node*)malloc(sizeof(Node));
 68         newNode->val = a;
 69         newNode->left = newNode->right = NULL;
 70         return newNode;
 71     }
 72     else if (a > root->val) {
 73         root->right = Insert(root->right, a);
 74     }
 75     else if (a < root->val) {
 76         root->left = Insert(root->left, a);
 77     }
 78     else {
 79         //这时候root的节点值等于a则不处理
 80     }
 81     return root;
 82 }
 83 Node* Delete(Node* root, int a) {
 84     if (!root) return root;
 85     if (a < root->val)       root->left = Delete(root->left, a);
 86     else if(a > root->val)   root->right = Delete(root->right, a);
 87     else{  //找到这个节点  那么进行删除操作  此刻要考虑四种情况:左右空 左右非空 左空右非空 左非空右空
 88         if (!root->left) { Node* temp = root; root = root->right; free(temp); }
 89         else if (!root->right) { Node* temp = root; root = root->left;free(temp); }
 90         else {
 91             Node* temp = root->right;
 92             while (temp->left) {
 93                 temp = temp->left;
 94             }
 95             temp->left = root->left;
 96             root = root->right;
 97         }
 98     }
 99     return root;//这一句实际上没有必要
100 }
101 Node* FindMin(Node* root) {
102     if (!root) {
103         return NULL;
104     }
105     if (!root->left) {
106         return root;
107     }
108     return FindMin(root->left);
109 }
110 Node* FindMax(Node* root) {
111     if (!root) {
112         return NULL;
113     }
114     if (!root->right) {
115         return root;
116     }
117     return FindMax(root->right);
118 }
119 void PreorderTraversal(Node* root) {
120     if (!root) return;
121     printf("%d  ", root->val);
122     PreorderTraversal(root->left);
123     PreorderTraversal(root->right);
124 }

 二叉搜索树--非递归--插入、删除和查找

  1 #define _CRT_SECURE_NO_WARNINGS
  2 //二叉查找树   非递归实现  插入,删除和查找
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 
  6 typedef struct Node {
  7     int val;
  8     struct Node* left;
  9     struct Node* right;
 10 }Node;
 11 typedef int bool;
 12 
 13 Node* Find(Node* root, int a);   //如果找到了返回指针 如果找不到则返回空指针
 14 Node* Insert(Node* root, int a);  //将a插入到root中并且返回根节点指针
 15 Node* Delete(Node* root, int a);   //将a从root中删除并且返回根节点指针
 16 Node* FindMin(Node* root);//找到最小节点并且返回该节点的指针
 17 Node* FindMax(Node* root);//找到最大节点并且返回该节点的指针
 18 void PreorderTraversal(Node* root); //先序遍历
 19 int main() {
 20     Node* root = (Node*)malloc(sizeof(Node));//根节点
 21     Node* BST, * MinP, * MaxP, * Tmp;   //分别是树根节点   最小节点 最大节点 和一个临时用的节点指针变量
 22     int X;
 23     int N, i;
 24     BST = NULL;
 25     scanf("%d", &N);//插入节点
 26     for (i = 0; i < N; i++) {
 27         scanf("%d", &X);
 28         BST = Insert(BST, X);
 29     }
 30     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍历
 31     MinP = FindMin(BST);
 32     MaxP = FindMax(BST);
 33     scanf("%d", &N);//查找节点
 34     for (i = 0; i < N; i++) {
 35         scanf("%d", &X);
 36         Tmp = Find(BST, X);
 37         if (Tmp == NULL) printf("%d is not found\n", X);
 38         else {
 39             printf("%d is found\n", Tmp->val);
 40             if (Tmp == MinP) printf("%d is the smallest key\n", Tmp->val);
 41             if (Tmp == MaxP) printf("%d is the largest key\n", Tmp->val);
 42         }
 43     }
 44     scanf("%d", &N);//删除节点
 45     for (i = 0; i < N; i++) {
 46         scanf("%d", &X);
 47         BST = Delete(BST, X);
 48     }
 49     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍历
 50     return 0;
 51 }
 52 Node* Find(Node* root, int a) { //查找
 53     if (!root) return NULL;
 54     while (root) {
 55         if (a == root->val) {
 56             break;
 57         }
 58         else if (a > root->val) {
 59             root = root->right;
 60         }
 61         else {//a< root->val
 62             root = root->left;
 63         }
 64     }
 65     return root;
 66 }
 67 Node* Insert(Node* root, int a) {
 68     if (!root) {
 69         root = (Node*)malloc(sizeof(Node));
 70         root->val = a;
 71         root->left = root->right = NULL;
 72     }
 73     else {
 74         Node* cur = root;
 75         Node* pre = NULL;
 76         while (cur) {
 77             if (a > cur->val) {
 78                 pre = cur;
 79                 cur = cur->right;
 80             }
 81             else if (a < cur->val) {
 82                 pre = cur;
 83                 cur = cur->left;
 84             }
 85             else {
 86                 break;//找到了值为a的节点
 87             }
 88         }
 89         if (!cur) {  //如果没有找到 则插入
 90             if (a < pre->val) {
 91                 pre->left = (Node*)malloc(sizeof(Node));
 92                 pre->left->left = pre->left->right = NULL;
 93                 pre->left->val = a;
 94             }
 95             else {
 96                 pre->right = (Node*)malloc(sizeof(Node));
 97                 pre->right->left = pre->right->right = NULL;
 98                 pre->right->val = a;
 99             }
100         }
101     }
102     return root;
103 }
104 Node* Delete(Node* root, int a) {//为什么代码这么长?因为没用到递归也没用到引用 就需要一个pre
105     if (!root) return root;//根节点为空
106     Node* pre = NULL;
107     Node* cur = root;
108     while (cur) {
109         if (a < cur->val) {
110             pre = cur;
111             cur = cur->left;
112         }
113         else if (a > cur->val) {
114             pre = cur;
115             cur = cur->right;
116         }
117         else {
118             if ((!cur->left) && (!cur->right)) {
119                 if (!pre) {//cur为根节点
120                     free(cur);
121                     return pre;
122                 }
123                 if (a < pre->val) {
124                     pre->left = NULL;
125                 }
126                 else {
127                     pre->right = NULL;
128                 }
129                 return root;
130             }
131             else if (!cur->right) {
132                 if (!pre) {
133                     cur = cur->left;
134                     free(root);
135                     return cur;
136                 }
137                 if (a < pre->val) {
138                     pre->left = cur->left;
139                     free(cur);
140                 }
141                 else {
142                     pre->right = cur->left;
143                     free(cur);
144                 }
145                 return root;
146             }
147             else if (!cur->left) {
148                 if (!pre) {
149                     cur = cur->right;
150                     free(root);
151                     return cur;
152                 }
153                 if (a < pre->val) {
154                     pre->left = cur->right;
155                     free(cur);
156                 }
157                 else {
158                     pre->right = cur->right;
159                     free(cur);
160                 }
161                 return root;
162             }
163             else {
164                 Node* temp = cur->left;
165                 while (temp->right) {
166                     temp = temp->right;
167                 }
168                 temp->right = cur->right;
169                 if (!pre) { Node* t = cur->left; free(cur); return t; };
170                 if (a < pre->val) {
171                     pre->left = cur->left;
172                 }
173                 else {
174                     pre->right = cur->left;
175                 }
176                 free(cur);
177                 return root;
178             }
179         }
180     }
181     return root;
182 }
183 Node* FindMin(Node* root) {
184     if (!root) {
185         return NULL;
186     }
187     while (root->left) {
188         root = root->left;
189     }
190     return root;
191 }
192 Node* FindMax(Node* root) {
193     if (!root) {
194         return NULL;
195     }
196     while (root->right) {
197         root = root->right;
198     }
199     return root;
200 }
201 void PreorderTraversal(Node* root) {
202     if (!root) return;
203     printf("%d  ", root->val);
204     PreorderTraversal(root->left);
205     PreorderTraversal(root->right);
206 }

 由前序中序重建二叉树

 由中序后序重建二叉树

【Morris_Traversal】二叉树遍历(前|中|后 序)--非递归--不用栈--(完整代码)

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #define DataType int
  5 #define nullptr 0
  6 #define MAXN 2048
  7 typedef struct node {
  8     DataType data;
  9     struct node* pleft;
 10     struct node* pright;
 11 }TNode, * TreeRoot;
 12 TNode* CreateTree() {//构建二叉树  (先序)
 13     int a;
 14     scanf("%d", &a);
 15     if (a != -1){
 16         TNode* ne = (TNode*)malloc(sizeof(TNode));
 17         ne->data = a;
 18         ne->pleft = CreateTree();
 19         ne->pright = CreateTree();
 20         return ne;
 21     }
 22     return nullptr;
 23 }
 24 void traversal_pre(TNode * root) { //先序遍历二叉树morris_traversal  (已过测试集)
 25     while (root) {
 26         if (root->pleft) {
 27             TNode* rightmost = root->pleft;
 28             while (rightmost->pright&&rightmost->pright!=root) {
 29                 rightmost = rightmost->pright;
 30             }
 31             if (rightmost->pright) {
 32                 rightmost->pright = nullptr;
 33                 root = root->pright;
 34             }
 35             else {
 36                 printf("%d ", root->data); //输出数据
 37                 rightmost->pright = root;
 38                 root = root->pleft;
 39             }
 40         }
 41         else {
 42             printf("%d ", root->data); //输出数据
 43             root = root->pright;
 44         }
 45     }
 46 }
 47 void traversal_mid(TNode* root) { //中序遍历二叉树morris_traversal  (已过测试集)
 48     while (root) {
 49         if (root->pleft) {
 50             TNode* rightmost = root->pleft;
 51             while (rightmost->pright && rightmost->pright != root) {
 52                 rightmost = rightmost->pright;
 53             }
 54             if (rightmost->pright) {
 55                 rightmost->pright = nullptr;
 56                 printf("%d ", root->data); //输出数据
 57                 root = root->pright;
 58             }
 59             else {
 60                 rightmost->pright = root;
 61                 root = root->pleft;
 62             }
 63         }
 64         else {
 65             printf("%d ", root->data); //输出数据
 66             root = root->pright;
 67         }
 68     }
 69 }
 70 void traversal_pos(TNode* root) { //后序遍历二叉树morris_traversal    (已过测试集)
 71     int temp[MAXN];
 72     int n = 0;
 73     while (root) {
 74         if (root->pright) {
 75             TNode* lm = root->pright;
 76             while (lm->pleft&&lm->pleft != root) {
 77                 lm = lm->pleft;
 78             }
 79             if (lm->pleft) {
 80                 lm->pleft = nullptr;
 81                 root = root->pleft;
 82             }
 83             else {
 84                 lm->pleft = root;
 85                 //printf("%d", root->data);
 86                 temp[n++] = root->data;
 87                 root = root->pright;
 88             }
 89         }
 90         else {
 91             temp[n++] = root->data;
 92             root = root->pleft;
 93         }
 94     }
 95     while (n) {
 96         printf("%d ", temp[--n]);
 97     }
 98 }
 99 int main() {
100     TNode * tree = CreateTree();  //创建二叉树
101     
102     //traversal_pre(tree);
103     //traversal_mid(tree);
104     traversal_pos(tree);
105     //system("pause");
106     return 0;
107 }
108 /*
109 空树: -1
110  ==============
111      1
112 
113 :1 -1 -1
114 先序遍历结果; 1
115 中序遍历结果: 1
116 后序遍历结果:1
117 =======================
118       1
119     2  
120 
121 :1 2 -1 -1 -1
122 先序遍历结果: 1  2
123 中序遍历结果: 2  1
124 后序遍历结果:2  1
125 =======================
126       1
127         2
128 
129 :1 -1 2 -1 -1
130 先序遍历结果: 1  2
131 中序遍历结果: 1  2
132 后序遍历结果:2  1
133 =======================
134        1
135     2      3
136       4   5  6
137 
138 :1 2 -1 4 -1 -1 3 5 -1 -1 6 -1 -1
139 先序遍历结果:1  2  4  3  5  6
140 中序遍历结果:2  4  1  5  3  6 
141 后序遍历结果: 4  2  5  6  3  1
142 */