This question is one of the few questions I have encountered while studying undergraduate courses. What is the manual construction process? The next traversal of the last node must be the root node of the entire tree. This element is found in the Middle-order traversal, the tree can be divided into two Subtrees. The left subtree is recursively constructed on the left of this element, and the right subtree is recursively constructed on the right. The element itself allocates space as the root node.
The difference between the set and map containers is that the vector container does not include the find member function, and the stl library function should be used. Fortunately, the returned iterator is also used, the iterators of the vector can be used for subtraction, And the offset can be easily obtained.
TreeNode * buildRec (vector <int> & inorder, int si, vector <int> & postorder, int so, int len) {if (len <= 0) return NULL; treeNode * root = new TreeNode (postorder [so]); int index = find (inorder. begin (), inorder. end (), postorder [so])-inorder. begin (); int newlen = index-si; root-> left = buildRec (inorder, si, postorder, so-len + newlen, newlen ); root-> right = buildRec (inorder, index + 1, postorder, so-1, len-newlen-1); return root;} class Solution {public: treeNode * buildTree (vector <int> & inorder, vector <int> & postorder) {return buildRec (inorder, 0, postorder, inorder. size ()-1, inorder. size ());}};