First Order + middle order and middle order + post order creation

Source: Internet
Author: User

Idea: the first element in the first and last elements in the latter order are the root of the current subtree, and then traverse the middle sequence to find the line between the left and right subtree, recursively create left and right subtree.

Class solution {public:/* because it is OJ, it is assumed that the given sequence is legal, normally, you need to determine the illegal situation */treenode * buildtree (vector <int> & inorder, vector <int> & postorder, int instart, int inend, int poststart, int postend) {treenode * root = new treenode (postorder [postend]); If (instart = inend & poststart = postend & instart = poststart) return root; int I; for (I = instart; I <= inend; ++ I) {If (inorder [I] = postorder [postend]) break; // locate the Middle Order Root Node} if (I> instart) Root-> left = buildtree (inorder, postorder, instart, I-1, poststart, poststart + (I-instart-1 )); if (I <inend) Root-> right = buildtree (inorder, postorder, I + 1, inend, poststart + I-instart, postend-1); Return root ;} treenode * buildtree (vector <int> & inorder, vector <int> & postorder) {int inlength = inorder. size (), postlength = postorder. size (); If (inlength = 0 | inleng Th! = Postlength) return NULL; return buildtree (inorder, postorder, 0, inlength-1, 0, postlength-1) ;}; class solution {public: treenode * buildtree (vector <int> & preorder, vector <int> & inorder, int prestart, int preend, int instart, int inend) {treenode * root = new treenode (preorder [prestart]); If (instart = inend & prestart = preend & instart = prestart) return root; int I; for (I = instart; I <= inend; ++ I ){ If (inorder [I] = preorder [prestart]) break; // locate the root node in the middle order} if (I> instart) Root-> left = buildtree (preorder, inorder, prestart + 1, prestart + I-instart, instart, I-1); if (I <inend) Root-> right = buildtree (preorder, inorder, prestart + I-instart + 1, preend, I + 1, inend); Return root;} treenode * buildtree (vector <int> & preorder, vector <int> & inorder) {int prelength = preorder. size (), inlength = inorder. Size (); If (inlength = 0 | inlength! = Prelength) return NULL; return buildtree (preorder, inorder, 0, prelength-1, 0, inlength-1 );}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.