16ms concise c++

  • 0
    class Solution {
            TreeNode *buildTree(std::vector<int> &preorder, std::vector<int> &inorder) {
                    return build(preorder.data(), inorder.data(), preorder.size());
            TreeNode *build(int *pp, int *pi, int n) {
                    if(n == 0) return NULL;
                    TreeNode *p = new TreeNode(pp[0]);
                    int i; for(i=0; i<n&&pi[i]!=pp[0]; i++);
                    p->left  = build(pp+1, pi, i);
                    p->right = build(pp+1+i, pi+1+i, n-i-1);
                    return p;

Log in to reply

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