C++ || BEATS 99% || INTUITIVE || SIMPLE RECURSION || EASY SOLUTION
Intuition and Approach\n Describe your first thoughts on how to solve this problem. \nSo first of all u must know the conditions for a constructing a unique bin
# Intuition and Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nSo first of all u must know the conditions for a constructing a unique binary tree that u must be given an inorder and a postorder/preorder/levelorder/anyother traversal to construct a unique binary tree\n\nThen next step is to figure out the root of the tree as we know the postorer traversal goes like left---->right---->root so the last element of the postorder msut be the root.\n\nSo,now u have the root okay so after u got the root what is the next thing u want left and right of the root, isn\'t it. Hey why don\'t u think what does inorder provides us its like left--->root--->right so if we find the root obtained from the postorder in the inorder vector then the nodes to the left of it would be the part of left subtree and the nodes at the right of it would be the part of right subtree.\n\nSo now u know the root of the tree the components of leftsubtree and the rightsubtree just try to extract the postorder and inorder of left and right subtree from the given vectors and just do the recursive calls for the left and the right subtree and u are good to go with the recursive solution.\n\n# Recursive code\n\n\n# Complexity\n- Time complexity: O(n^2)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n   \n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n    class Solution {\n    public:\n    #include <algorithm>\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        if(postorder.empty()) return nullptr;\n        \n        int n=postorder.size();\n        int root_val=postorder[n-1];\n        TreeNode* root=new TreeNode(root_val);\n        int index=find(inorder.begin(),inorder.end(),root_val)-inorder.begin();\n\n        vector<int> linorder(inorder.begin(),inorder.begin()+index);\n        vector<int> lpostorder(postorder.begin(),postorder.begin()+index);\n        \n        TreeNode* left=buildTree(linorder,lpostorder);\n        root->left=left;\n\n        vector<int> rinorder(inorder.begin()+index+1,inorder.end());\n        vector<int> rpostorder(postorder.begin()+index,postorder.end()-1);\n\n        TreeNode* right=buildTree(rinorder,rpostorder);\n        root->right=right;\n        return root;}};\n\n# Optimized code\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n   \n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```  \nclass Solution {\npublic:\n    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {\n        if (postorder.empty()) return nullptr;\n\n        int postIndex = postorder.size() - 1; // Start with the last element of postorder\n        return buildTreeHelper(inorder, postorder, postIndex, 0, inorder.size() - 1);\n    }\n\n    TreeNode* buildTreeHelper(vector<int>& inorder, vector<int>& postorder, int& postIndex, int inStart, int inEnd) {\n        if (inStart > inEnd) return nullptr;\n\n        int rootVal = postorder[postIndex];\n        TreeNode* root = new TreeNode(rootVal);\n        postIndex--;\n\n        int rootIndex = findIndex(inorder, rootVal, inStart, inEnd);\n\n        root->right = buildTreeHelper(inorder, postorder, postIndex, rootIndex + 1, inEnd);\n        root->left = buildTreeHelper(inorder, postorder, postIndex, inStart, rootIndex - 1);\n\n        return root;\n    }\n\n    int findIndex(vector<int>& inorder, int target, int start, int end) {\n        for (int i = start; i <= end; i++) {\n            if (inorder[i] == target)\n                return i;\n        }\n        return -1; // Not found\n    }\n};\n\n\n```
['C++']