C++ and Python3 || Hashmap + Recursion || Optimal Approach
\n\n# Code\nC++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     T
> ![image.png](https://assets.leetcode.com/users/images/19272698-48a1-45a2-a742-4c34071826ed_1708697632.4237342.png)\n\n# Code\n```C++ []\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* helper(vector<int> &inorder, int is, int ie, vector<int> &postorder, int ps, int pe, unordered_map<int, int> &inOrder) {\n        if(ps > pe || is > ie) return NULL;\n        TreeNode* root = new TreeNode(postorder[pe]);\n        int inorderRoot = inOrder[root->val];\n        int left = inorderRoot - is;\n        root->left = helper(inorder, is, inorderRoot - 1,\n        postorder, ps, ps + left - 1, inOrder);\n        root->right = helper(inorder, inorderRoot + 1, ie,\n        postorder, ps + left, pe - 1, inOrder);\n        return root;\n    }\n\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        unordered_map<int, int> inOrder;\n        for(int i=0; i<inorder.size(); i++) {\n            inOrder[inorder[i]] = i;\n        }\n        TreeNode* root = helper(inorder, 0, inorder.size() - 1,\n        postorder, 0, postorder.size() - 1, inOrder);\n        return root;\n    }\n};\n```\n```Python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n        inorderMap = {}\n        for i in range(len(inorder)):\n            inorderMap[inorder[i]] = i\n        def helper(l, r):\n            if l > r:\n                return None\n            root = TreeNode(postorder.pop())\n            mid = inorderMap[root.val]\n            root.right = helper(mid + 1, r)\n            root.left = helper(l, mid - 1)\n            return root\n        root = helper(0, len(inorder) - 1)\n        return root\n```
['Hash Table', 'Divide and Conquer', 'Tree', 'Recursion', 'Binary Tree', 'C++', 'Python3']