Переписать код C в Java, чтобы создать полное двоичное дерево

Я хочу написать функцию для построения полного двоичного дерева из заданного массива preorder и postorder. Я нашел эту ссылкуhttp://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-traversals/ который предлагает следующий код C:

 struct node* constructTreeUtil (int pre[], int post[], int* preIndex,
                            int l, int h, int size)
{
// Base case
if (*preIndex >= size || l > h)
    return NULL;

// The first node in preorder traversal is root. So take the node at
// preIndex from preorder and make it root, and increment preIndex
struct node* root = newNode ( pre[*preIndex] );
++*preIndex;

// If the current subarry has only one element, no need to recur
if (l == h)
    return root;

// Search the next element of pre[] in post[]
int i;
for (i = l; i <= h; ++i)
    if (pre[*preIndex] == post[i])
        break;

// Use the index of element found in postorder to divide postorder array in
// two parts. Left subtree and right subtree
if (i <= h)
{
    root->left = constructTreeUtil (pre, post, preIndex, l, i, size);
    root->right = constructTreeUtil (pre, post, preIndex, i + 1, h, size);
}

return root;
}

 // The main function to construct Full Binary Tree from given preorder and 
// postorder traversals. This function mainly uses constructTreeUtil()
struct node *constructTree (int pre[], int post[], int size)
{
int preIndex = 0;
return constructTreeUtil (pre, post, &preIndex, 0, size - 1, size);
}

Я пытался переписать этот код на Java. Вот мой код:

private static TreeNode constructTree(int[] preorder, int[] postorder, Index index, int lowIndex, int highIndex){

    // Base case
    if (index.index >= preorder.length || lowIndex > highIndex){
        return null;
    }

      // The first node in preorder traversal is root. So take the node at
      // preIndex from preorder and make it root, and increment preIndex
    TreeNode root = new TreeNode (preorder[lowIndex]);
    index.index++;

      // If the current subarry has only one element, no need to recur
    if (lowIndex == highIndex){
        return root;
    }

      // Search the next element of pre[] in post[]
    int i = 0;
    for (i = lowIndex; i <= highIndex; ++i)
        if (preorder[i]== postorder[lowIndex])
            break;

    // Use the index of element found in postorder to divide postorder array in
    // two parts. Left subtree and right subtree
        if (i <= highIndex) {
            root.left = constructTree(preorder, postorder, index, lowIndex, i);
            root.right = constructTree(preorder, postorder, index, i + 1, highIndex);
        }
        return root;
                    }

    //The main function to construct Full Binary Tree from given preorder and 
    //postorder traversals. This function mainly uses constructTreeUtil()
public static TreeNode constructTree (int preorder[], int postorder[]) {
    return constructTree (preorder, postorder, new Index(), 0, preorder.length - 1);
   }

Но я получил непрерывный цикл в корневом узле (он не перешел на другие узлы, которые должны быть его дочерними). Можете ли вы помочь мне, пожалуйста, чтобы увидеть, где ошибка в моем коде Java?

Я не совсем уверен, но я думаю, что ошибка может быть из-за этих строк:

    int i = 0;
    for (i = lowIndex; i <= highIndex; ++i)
        if (preorder[i]== postorder[lowIndex])
            break;

Я не очень хорошо понимал соответствующие строки в исходном C-коде. Особенно в этой части

Ответы на вопрос(3)

Ваш ответ на вопрос