# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Typedef struct Node
{
Char value;
Struct node * leftchild;
Struct node * rightchild;
Struct node * parent;
Int depth;
} Treenode;
// Construct a binary tree based on the central and forward Sequences
Treenode * createfrommidpre (char mid [], int midleft, int midright, char pre [], int preleft, int preright, treenode * parent)
{
Treenode * node = (treenode *) malloc (sizeof (treenode ));
Int midleft1, midright1, preleft1, preright1, rootpos = 0;
If (midright-midleft! = Preright-preleft)
{
Printf ("error: the number of nodes in the input middle sequence is different from that in the front sequence! \ N ");
Return node;
}
Node-> value = pre [preleft];
Node-> leftchild = NULL;
Node-> rightchild = NULL;
Node-> parent = parent;
If (parent = NULL)
Node-> depth = 0;
Else
Node-> depth = parent-> depth + 1;
For (; Mid [rootpos]! = Pre [preleft]; rootpos ++ );
If (rootpos! = Midleft)
{
Midright1 = rootPos-1;
Preright1 = rootpos-midleft + preleft;
Node-> leftchild = createfrommidpre (MID, midleft, midright1, pre, preleft + 1, preright1, node );
}
If (rootpos! = Midright)
{
Midleft1 = rootpos + 1;
Preleft1 = rootpos-midleft + preleft + 1;
Node-> rightchild = createfrommidpre (MID, midleft1, midright, pre, preleft1, preright, node );
}
Return node;
}
// Give the node value to get the pointer to the node
Treenode * Find (treenode * root, char data)
{
If (root = NULL)
Return NULL;
If (root-> value = data)
Return root;
Else if (find (root-> leftchild, data )! = NULL)
Return find (root-> leftchild, data );
Else if (find (root-> rightchild, data )! = NULL)
Return find (root-> rightchild, data );
Else
Return NULL;
}
// The main idea is to get the depth of the two nodes first, and the loop of the deeper node Pointer Points to the parent node until the two nodes are at the same layer,
// Then the two pointers point to the parent node cyclically at the same time, knowing that the two pointers meet
Void sameprenode (treenode * root, int data1, int data2)
{
Treenode * P1, * P2;
Int differ, I;
P1 = find (root, data1 );
P2 = find (root, data2 );
If (p1 = NULL | P2 = NULL)
{
Printf ("input nodes are not all in the tree \ n ");
Return;
}
If (P1-> depth> P2-> depth)
{
Differ = p1-> depth-p2-> depth;
For (I = 0; I <differ; I ++)
{
P1 = p1-> parent;
}
}
Else if (P1-> depth <P2-> depth)
{
Differ = P2> depth-p1> depth;
For (I = 0; I <differ; I ++)
{
P2 = P2-> parent;
}
}
While (P1! = P2)
{
P1 = p1-> parent;
P2 = P2-> parent;
}
Printf ("recent public node: % C \ n", P1-> value );
}
Int main ()
{
Treenode * root1;
Char preseq [20], midseq [20], data1, data2;
Int I = 0, numnode;
Printf ("Enter the total number of nodes: \ n ");
Scanf ("% d", & numnode );
Fflush (stdin );
Printf ("Please input the first sequence: \ n ");
For (I = 0; I <numnode; I ++)
Scanf ("% C", preseq + I );
Fflush (stdin );
Printf ("Please input the central sequence: \ n ");
For (I = 0; I <numnode; I ++)
Scanf ("% C", midseq + I );
Fflush (stdin );
Root1 = createfrommidpre (midseq, 0, I-1, preseq, 0, I-1, null );
While (1)
{
Printf ("Enter the two nodes you want to find: \ n ");
Scanf ("% C", & data1, & data2 );
Fflush (stdin );
Sameprenode (root1, data1, data2 );
If (data1 = 0)
Break;
}
Return 1;
}