@zxyperfect said in Sharing my straightforward recursive solution:

`TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { return create(preorder, inorder, 0, preorder.size() - 1, 0, inorder.size() - 1); } TreeNode* create(vector<int>& preorder, vector<int>& inorder, int ps, int pe, int is, int ie){ if(ps > pe){ return nullptr; } TreeNode* node = new TreeNode(preorder[ps]); int pos; for(int i = is; i <= ie; i++){ if(inorder[i] == node->val){ pos = i; break; } } node->left = create(preorder, inorder, ps + 1, ps + pos - is, is, pos - 1); node->right = create(preorder, inorder, pe - ie + pos + 1, pe, pos + 1, ie); return node; }`

The first element in preorder array can divide inorder array into two parts. Then we can divide preorder array into two parts. Make this element a node. And the left sub-tree of this node is the left part, right sub-tree of this node is the right part. This problem can be solved following this logic.

I want to ask my program is similar with yours, but when the 202 case, it will stoped because Memory Limit Exceeded... so do you have the same problem?