My simple c++ solution

  • 0
    class Solution {
    TreeNode * helper(vector<int>& preorder, vector<int>& inorder, int start, int end)
        if(start>end)return NULL;
        TreeNode * root=new TreeNode(preorder.front());
        if(start==end)return root;
        int key=find(inorder.begin(), inorder.end(), root->val) - inorder.begin();
        root->left=helper(preorder, inorder, start, key-1);
        root->right=helper(preorder, inorder, key+1, end);
        return root;
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        return helper(preorder, inorder, 0, preorder.size()-1);


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.