Java iterative solution with explanation
\n    public TreeNode buildTree(int[] inorder, int[] postorder) {\n        if (inorder.length == 0 || postorder.length == 0) return null;\n        int ip = inor
\n    public TreeNode buildTree(int[] inorder, int[] postorder) {\n        if (inorder.length == 0 || postorder.length == 0) return null;\n        int ip = inorder.length - 1;\n        int pp = postorder.length - 1;\n        \n        Stack<TreeNode> stack = new Stack<TreeNode>();\n        TreeNode prev = null;\n        TreeNode root = new TreeNode(postorder[pp]);\n        stack.push(root);\n        pp--;\n        \n        while (pp >= 0) {\n            while (!stack.isEmpty() && stack.peek().val == inorder[ip]) {\n                prev = stack.pop();\n                ip--;\n            }\n            TreeNode newNode = new TreeNode(postorder[pp]);\n            if (prev != null) {\n                prev.left = newNode;\n            } else if (!stack.isEmpty()) {\n                TreeNode currTop = stack.peek();\n                currTop.right = newNode;\n            }\n            stack.push(newNode);\n            prev = null;\n            pp--;\n        }\n        \n        return root;\n    }\n\nThis is my iterative solution, think about "Constructing Binary Tree from inorder and preorder array", the idea is quite similar. Instead of scanning the preorder array from beginning to end and using inorder array as a kind of mark, in this question, the key point is to scanning the postorder array from end to beginning and also use inorder array from end to beginning as a mark because the logic is more clear in this way. ***The core idea is: Starting from the last element of the postorder and inorder array, we put elements from postorder array to a stack and each one is the right child of the last one until an element in postorder array is equal to the element on the inorder array. Then, we pop as many as elements we can from the stack and decrease the mark in inorder array until the peek() element is not equal to the mark value or the stack is empty. Then, the new element that we are gonna scan from postorder array is the left child of the last element we have popped out from the stack.***
['Java']