Recursive implementation of pre-order and middle-order construction trees

Source: Internet
Author: User

Although sometimes the efficiency of recursion is not high, but can be simplified to make the problem of the efficiency greatly improved. It is an important skill to master in program design.

In this case, the most important of these are three aspects:

(1) How to design a recursive body

(2) How recursive parameters are passed

(3) How to write a recursive exit to simplify the program


First, the form of the recursive body determines whether the program can be relatively simple, if the details are too cumbersome to consider, then the use of recursive program will also write very painful. Because you have to the computer automatically completed the task too much to the oneself. So, before you write, be sure to do a top-level design. In this case, two unordered_map were designed to quickly determine the extent of the subtree and set it to global to avoid the trouble of parameter passing.


Secondly, what to preach, how to preach? When the recursive body is designed, it is OK to wear anything. Larger entities can be passed in a referenced way, such as a process without modification plus a const.


Finally, if a recursive exit is determined. Be sure to think of the simplest way to avoid overly dealing with details.

/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {Public:treenode *sub (vector<int> &preorder, int pre1,int pre2,vector<int> &inord Er,int In1,int in2) {if (pre2<pre1) return nullptr; <span style= "White-space:pre" ></span>//very impor        Tant to assure when the recursion ends TreeNode *root = new TreeNode (Preorder[pre1]);        inorder ranges int l1=in1, l2=in[preorder[pre1]]-1;        int r1=l2+2, r2=in2; Preoder ranges int ll=pre1+1, lr=pre1+1+ (L2-L1);                L2-l1,length of left subtree int rl=lr+1, rr=pre2;        Root->left=sub (PREORDER,LL,LR,INORDER,L1,L2);                Root->right=sub (PREORDER,RL,RR,INORDER,R1,R2);    return root; } TreeNode *buildtree (vector<int> &preorder, vector<int> &inorder) {if (!preorDer.size () | |!        Inorder.size ()) return nullptr;        if (Preorder.size ()!=inorder.size ()) return nullptr;        pre-processing for (int i=0; i< preorder.size (); ++i) pre[preorder[i]]=i;                for (int i=0; i< inorder.size (); ++i) in[inorder[i]]=i;                int N=preorder.size ()-1;                TreeNode *root=sub (preorder,0,n,inorder,0,n);    return root; } public:unordered_map<int,int> pre,in;};



Recursive implementation of pre-order and middle-order construction trees

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.