Assume that the post-order traversal sequence of a binary tree is DGJHEBIFCA, the middle-order traversal sequence is DBGEHJACIF, and the pre-order traversal is obtained.
The overall idea is as follows: locate each node through the back-order traversal, then judge the Left and Right Subtrees by the middle-order traversal, restore the entire Binary Tree, and write the forward-order traversal.
The order of post-order traversal is known. The last A is the root node of the binary tree,
Then, divide the Middle-order traversal from A into two sections. The left part of A is the left subtree, And the right part of A is the right subtree,
The result is as follows:
Then look at the subtree on the right,
We can see from the post-order traversal that the post-order traversal of the Left subtree is IFC, and the middle-order traversal is CIF.
Back to the beginning, repeat the previous process. The root node is C in the descending traversal, and the middle traversal is divided into two segments from C,
Left is the left subtree, right is the right subtree
That is, there is only one right subtree on the right,
Repeat the preceding process again. Now, the IF post-order traversal is IF, and the middle-order traversal is IF.
Node F, I is the left subtree of F
In this way, the right subtree of this binary tree is completely restored, and the method of the Left subtree is exactly the same, which is a recursive process. The flowchart is as follows:
NEXT:
The Complete Binary Tree obtained at the end is as follows:
Then write the pre-order traversal, Which is ABDEGHJCFI.
It can be implemented using algorithms and remains here for the moment.
Author: jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)