The premise is that the values of each node are different.
The idea is that the first value of the forward traversal is the value of the parent node. You can narrow the range by finding the value in the middle traversal. Use recursion.
Step 5:
1. Create a root node based on the first value of the table in the previous order. If there is only one node, the result will be returned as a result of recursive termination.
2. Find the parent node location in the middle-order table
3. If there is a left subtree, create the left subtree recursively and assign the return value to the left subtree pointer.
4. If the right subtree exists, create the right subtree recursively and assign the return value to the right subtree pointer.
5. Return the root node
# Include <stdio. h> # include <stdlib. h> typedef struct mynode {int a; struct mynode * leftpoint; struct mynode * rightpoint;} node, * nodepoint; nodepoint construct (int * pre_start, int * pre_end, int * mid_start, int * mid_end) {// build the parent node int root_value = pre_start [0]; nodepoint root = (nodepoint) malloc (sizeof (node); root-> a = root_value; root-> leftpoint = root-> rightpoint = NULL; if (pre_start = pre_end && Mid_start = mid_end & * pre_start = * mid_start) // return root for Recursive termination; // search for the root node int * mid_root = mid_start in the central sequence table; while (mid_root <= mid_end & * mid_root! = Root_value) ++ mid_root; if (mid_root = mid_end & * mid_root! = Root_value) exit (0); // recursive int mid_left_end = mid_root-mid_start; int * pre_left_end = pre_start + mid_left_end; if (mid_left_end> 0) {// left subtree, build left subtree root-> leftpoint = construct (pre_start + 1, pre_left_end, mid_start, mid_root-1);} if (mid_left_end <pre_end-pre_start) {// right subtree available, build right subtree root-> rightpoint = construct (pre_left_end + 1, pre_end, mid_root + 1, mid_end);} return root; // return the root node} void Print_tree_pre (nodepoint p) {if (! P) return; else {printf ("% d \ t", p-> a); print_tree_pre (p-> leftpoint); print_tree_pre (p-> rightpoint );}} void print_tree_mid (nodepoint p) {if (p = NULL) return; else {print_tree_mid (p-> leftpoint); printf ("% d \ t ", p-> a); print_tree_mid (p-> rightpoint) ;}} void print_tree_aft (nodepoint p) {if (p = NULL) return; else {print_tree_aft (p-> leftpoint); print_tree_aft (p-> rightpoint); printf ("% d \ t", p-> a) ;}} void main () {int a [8] = {,}, B [8] = {,}; nodepoint node; node = construct (a, a + 7, B, B + 7); print_tree_pre (node); printf ("\ n"); print_tree_mid (node ); printf ("\ n"); print_tree_aft (node); printf ("\ n ");}