Home > Back-end >  For bosses to help me take a look at this period of binary tree after Morris sequence traversal code
For bosses to help me take a look at this period of binary tree after Morris sequence traversal code

Time:10-11

//binary tree sequence traversal functions after Morris

Struct TreeNode
{
int val;
Struct TreeNode * left;
Struct TreeNode * right;
};

Void reverse (struct TreeNode * from, struct TreeNode * to)
{
Struct TreeNode * x, y, z;

If (the from==to) return;

X=the from;
Y=the from - & gt; Right;
The from - & gt; Right=NULL;
While (x!=to) {
Z=y - & gt; Right;
Y - & gt; Right=x;
X=y;
Y=z;
}
}

Void printreverse (struct TreeNode * from, struct TreeNode * to)
{
Struct TreeNode * p;
Reverse (the from and to);

P=to;
While (1) {
Printf (" % d \ n ", p - & gt; Val);
If (p==the from) break;
P=p - & gt; Right;
}

Reverse (to and from);
}

Void postorderTraverse (struct root TreeNode *)
{
Struct TreeNode * p=NULL;
Struct TreeNode * t1=malloc (sizeof (struct TreeNode));
T1 - & gt; Left=root;
T1 - & gt; Right=NULL;
Struct TreeNode * t=t1;

While (t) {
If (t - & gt; Left) {
P=t - & gt; The left;
While (p - & gt; Right!=NULL & amp; & P - & gt; Right! T)={
P=p - & gt; Right;
}

If (p - & gt; Right==NULL) {
P - & gt; Right=t;
T=t - & gt; The left;
}
The else {
Printreverse (t - & gt; Left, p);
P - & gt; Right=NULL;
T=t - & gt; Right;
}
}
The else {
T=t - & gt; Right;
}
}
Free (t1).
}

This code I tested on the compiler, didn't find the wrong place, but I feel code is too long? what is the need to improve?
  • Related