Best O(N) Solution
Approach\nOptimal\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct Tree
# Approach\nOptimal\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    TreeNode* build(int postStart, int postEnd, vector<int>& postorder, \n    int inStart, int inEnd, vector<int>& inorder, unordered_map <int, int>& mp) {\n        if (postStart > postEnd || inStart > inEnd)\n            return NULL;\n        TreeNode* root = new TreeNode(postorder[postEnd]);\n        int ind = mp[postorder[postEnd]];    \n        root->left = build(postStart, postStart+ind-inStart-1, postorder, inStart, ind-1, inorder, mp);\n        root->right = build(postStart+ind-inStart, postEnd-1, postorder, ind+1, inEnd, inorder, mp);\n\n        return root;   \n    }\n\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        int postStart = 0, postEnd = postorder.size() - 1;\n        int inStart = 0, inEnd = inorder.size() - 1;\n        unordered_map <int, int> mp;\n        for (int i = 0; i < inorder.size(); i++)\n            mp[inorder[i]] = i;\n\n        return build(postStart, postEnd, postorder, inStart, inEnd, inorder, mp);    \n    }\n};\n```
['C++']