Best Solution for Trees 🚀 in C++, Python and Java || 100% working
🧠 IntuitionWe are given inorder and postorder traversals.
In postorder, the last node is always the root.
From the root's index in inorder, we can split left an

# 🧠 Intuition
We are given `inorder` and `postorder` traversals.  
In postorder, the **last node is always the root**.  
From the root's index in inorder, we can split left and right subtrees.  
We build the tree using this info **recursively** 🔁🌳

---

# 🚀 Approach
- Start from the **last element** of postorder.
- Use a **hashmap** to quickly find the index of elements in inorder.
- Build **right subtree first**, then left (because postorder is reversed).

---

# ⏱️ Complexity
- Time complexity: $$O(n)$$  
- Space complexity: $$O(n)$$

---

# 💻 Code
```cpp []
class Solution {
public:
    unordered_map<int,int> map;
    int idx;

    TreeNode* BuildTree(vector<int>& inorder, vector<int>& postorder, int s, int e) {
        if(s > e) return nullptr;
        int curr = postorder[idx--];
        TreeNode* root = new TreeNode(curr);
        int id = map[curr];
        root->right = BuildTree(inorder, postorder, id + 1, e);
        root->left = BuildTree(inorder, postorder, s, id - 1);
        return root;
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        for(int i = 0; i < inorder.size(); i++) map[inorder[i]] = i;
        idx = postorder.size() - 1;
        return BuildTree(inorder, postorder, 0, inorder.size() - 1);
    }
};
```

```python []
class Solution:
    def buildTree(self, inorder, postorder):
        idx = len(postorder) - 1
        mp = {val: i for i, val in enumerate(inorder)}
        
        def build(s, e):
            nonlocal idx
            if s > e:
                return None
            root_val = postorder[idx]
            idx -= 1
            root = TreeNode(root_val)
            id = mp[root_val]
            root.right = build(id + 1, e)
            root.left = build(s, id - 1)
            return root
        
        return build(0, len(inorder) - 1)
```

```java []
class Solution {
    Map<Integer, Integer> map = new HashMap<>();
    int idx;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i = 0; i < inorder.length; i++) map.put(inorder[i], i);
        idx = postorder.length - 1;
        return build(postorder, inorder, 0, inorder.length - 1);
    }

    private TreeNode build(int[] postorder, int[] inorder, int s, int e) {
        if(s > e) return null;
        int curr = postorder[idx--];
        TreeNode root = new TreeNode(curr);
        int id = map.get(curr);
        root.right = build(postorder, inorder, id + 1, e);
        root.left = build(postorder, inorder, s, id - 1);
        return root;
    }
}
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Array', 'Hash Table', 'Divide and Conquer', 'Tree', 'Binary Tree', 'Python', 'C++', 'Java', 'Python3']