(C++) 106. Construct Binary Tree from Inorder and Postorder Traversal
\n\nclass Solution {\npublic:\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        unordered_map<int, int> mp; \n        for (int i
\n```\nclass Solution {\npublic:\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        unordered_map<int, int> mp; \n        for (int i = 0; i < inorder.size(); ++i) mp[inorder[i]] = i; \n        \n        stack<TreeNode*> stk; \n        TreeNode *root = nullptr, *node = nullptr; \n        for (int i = postorder.size()-1; i >= 0; --i) {\n            int x = postorder[i]; \n            if (!root) root = node = new TreeNode(x); \n            else if (mp[node->val] < mp[x]) {\n                stk.push(node); \n                node = node->right = new TreeNode(x); \n            } else {\n                while (stk.size() && mp[x] < mp[stk.top()->val]) node = stk.top(), stk.pop(); \n                node = node->left = new TreeNode(x); \n            }\n        }\n        return root;\n    }\n};\n```
['C']