Rewrite a C code in Java to construct full binary tree

情到浓时终转凉″ 提交于 2019-12-04 05:10:37

Here are the bugs:

  1. Line if (preorder[i]== postorder[lowIndex]) has two errors: the first is that you search in preorder instead of in postorder, and the second is that you use lowIndex instead of preIndex. This line should be: if (preorder[index.index]== postorder[i])
  2. Line TreeNode root = new TreeNode (preorder[lowIndex]); - lowIndex is used again instead of preIndex. This line should be: TreeNode root = new TreeNode (preorder[index.index]);

Pay attention to the fact that this code would work only for full binary trees

Assuming that your code written in C works, then my guess is that for this part

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

you'd want to use the same variables that you use in the C version. In this case, your if statement should be

if (preorder[index.index]== postorder[i])

First make your TreeNode into a class, since it is the java equivalent of a struct

public class TreeNode{
    public int val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int val){
        this.val = val;
    }
}    

Then make your TreeUtil class

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

        // Base case
        if (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[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[]
        for (int i = lowIndex; i <= highIndex; i++)
            if (preorder[index]== postorder[i]){
                // Use the index of element found in postorder to divide postorder array in
                // two parts. Left subtree and right subtree
                root.left = constructTree(preorder, postorder, index, lowIndex, i);
                root.right = constructTree(preorder, postorder, index, i + 1, highIndex);
                break;
            }
        }
        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, 0, 0, preorder.length - 1);
    }
}

Errors in your code were if (preorder[i]== postorder[lowIndex] and TreeNode root = new TreeNode (preorder[lowIndex]); as mentioned by aviad. I changed your code for you to be slightly less confusing. Using the Index is a great way to confuse yourself especially since an int will work fine. Also ++i increments before you run the loop and isn't conventionally used in java and java lets you declare variables a part of your loop definition so I changed that for loop to be more what your are looking for.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!