public TreeNode buildTree(int[] inorder, int[] postorder) {

    if (inorder.length == 0 || postorder.length == 0) {
      return null;
    }

    int rootVal = postorder[postorder.length - 1];
    TreeNode root = new TreeNode(rootVal);

    int rootIdxInorder = 0;
    for (int i = 0 ; i < inorder.length ; i++) {
      if (inorder[i] == rootVal) {
        rootIdxInorder = i;
        break;
      }
    }

    root.left = buildTree(Arrays.copyOfRange(inorder, 0, rootIdxInorder), Arrays.copyOfRange(postorder, 0, rootIdxInorder));
    root.right = buildTree(Arrays.copyOfRange(inorder, rootIdxInorder + 1, inorder.length), Arrays.copyOfRange(postorder, rootIdxInorder, postorder.length - 1));

    return root;
  }