✅ Concise Java Soution Explained 🚀 | Beats 100% submissions
Intuition\nInorder is    L Root R\nPostorder is  L  R   Root\n\nSo the last element of the post order traversal will give us the root\nwe can find the root elem
# Intuition\nInorder is    L Root R\nPostorder is  L  R   Root\n\nSo the last element of the post order traversal will give us the root\nwe can find the root element index in inorder to indentify the root elements left and right subtree\n\ninorder = [9,**3**,15,20,7]\npostorder = [9,15,7,20, **3** ]\n\n----\n#root is 3\nso index 0 to 0(rootIndex-1) is left subtree that is 9\nand index 2(rootIndex+1) to 4 in inorder is right subtree\n\nso we divided the sub problem in sub-parts and now can recursively call it\n\n----\n**NOTE**\nIn my case i have called right subtree first while solving and have not considered start and end of postorderIndex is because we know that in post traversal order L R Root . If we have solved for root next element would be right(i.e right subtree) and then left(i.e left subtree), so that is the reason.\n\n\n- Time complexity:\nO(n) since we visiting every element of array\n\n- Space complexity\nO(n) storing data inorder data in hashmap\n\n# Code\n```\nclass Solution {\n    int postorderIndex;\n    Map<Integer, Integer> inorderIndexMap;\n    public TreeNode buildTree(int[] inorder, int[] postorder) {\n        postorderIndex = postorder.length - 1;\n        // build a hashmap to store value -> its index relations\n        inorderIndexMap = new HashMap<>();\n        for (int i = 0; i < inorder.length; i++) {\n            inorderIndexMap.put(inorder[i], i);\n        }\n\n        return arrayToTree(postorder, 0, postorder.length - 1);\n    }\n\n    private TreeNode arrayToTree(int[] postorder, int left, int right) {\n        // if there are no elements to construct the tree\n        if (left > right) return null;\n\n        // select the postorder_index element as the root and decrement it\n        int rootValue = postorder[postorderIndex--];\n        TreeNode root = new TreeNode(rootValue);\n\n        // build right and left subtree\n        // excluding inorderIndexMap[rootValue] element because it\'s the root\n        root.right = arrayToTree(postorder, inorderIndexMap.get(rootValue) + 1, right);\n        root.left = arrayToTree(postorder, left, inorderIndexMap.get(rootValue) - 1);\n        return root;\n    }\n}\n```
['Recursion', 'Java']