Bootstrap

从前序与中序遍历序列构造二叉树-力扣

与后序与中序遍历构造类似

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* subVec(vector<int> preorder, vector<int> inorder){
        if(preorder.empty() && inorder.empty()){
            return nullptr;
        }
        TreeNode * root = new TreeNode(preorder[0]);
        if(preorder.size() == 1){
            return root;
        }
        int flag = 0;
        for(flag; flag < inorder.size(); flag++){
            if(inorder[flag] == root->val){
                break;
            }
        }
        vector<int> preorderleft;
        vector<int> preorderright;
        vector<int> inorderleft;
        vector<int> inorderright;
        for(int i = 0; i < flag; i++){
            preorderleft.push_back(preorder[i + 1]);
            inorderleft.push_back(inorder[i]);
        }
        for(int j = flag + 1; j < inorder.size(); j++){
            preorderright.push_back(preorder[j]);
            inorderright.push_back(inorder[j]);
        }

        root->left = subVec(preorderleft, inorderleft);
        root->right = subVec(preorderright, inorderright);
        return root;
    }
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        TreeNode* root = subVec(preorder, inorder);
        return root;
    }
};

同样的对代码进行优化:

class Solution {
private:
        TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& preorder, int preorderBegin, int preorderEnd) {
        if (preorderBegin == preorderEnd) return NULL;

        int rootValue = preorder[preorderBegin];
        TreeNode* root = new TreeNode(rootValue);

        if (preorderEnd - preorderBegin == 1) return root;

        int delimiterIndex;
        for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
            if (inorder[delimiterIndex] == rootValue) break;
        }

        int leftInorderBegin = inorderBegin;
        int leftInorderEnd = delimiterIndex;

        int rightInorderBegin = delimiterIndex + 1;
        int rightInorderEnd = inorderEnd;

        int leftPreorderBegin =  preorderBegin + 1;
        int leftPreorderEnd = preorderBegin + 1 + delimiterIndex - inorderBegin; 

        int rightPreorderBegin = preorderBegin + 1 + (delimiterIndex - inorderBegin);
        int rightPreorderEnd = preorderEnd;

        root->left = traversal(inorder, leftInorderBegin, leftInorderEnd,  preorder, leftPreorderBegin, leftPreorderEnd);
        root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, preorder, rightPreorderBegin, rightPreorderEnd);

        return root;
    }

public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if (inorder.size() == 0 || preorder.size() == 0) return NULL;
        return traversal(inorder, 0, inorder.size(), preorder, 0, preorder.size());
    }
};
;