0

This was the problem to build binary tree using preorder and inorder

And I get

WARNING:A command line option has enabled the Security Manager

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

    Map<Integer,Integer> inorder_map=new HashMap<Integer,Integer>();
    for(int i=0;i<inorder.length-1;i++)
    {
        inorder_map.put(inorder[i],i);
    }
    TreeNode root=buildTreeHelp(preorder,0,preorder.length-1,inorder,0,inorder.length-1,inorder_map);
    return root;
}

public  TreeNode buildTreeHelp(int[] preOrder,int preStart,int preEnd,int[] inOrder,int inStart,int inEnd,Map<Integer , Integer> inorder_Map)
{
    
    if(preStart>preEnd||inStart>inEnd)
    {
        return null;
    }
    
    TreeNode root =new TreeNode(preOrder[preStart]);
    
    int inRoot=inorder_Map.get(root.val); // where I get warning
    int nums_in_left=inRoot-inStart;
    
    root.left=buildTreeHelp(preOrder,preStart+1,preStart+nums_in_left,inOrder,inStart,inRoot-1,inorder_Map);
    root.right=buildTreeHelp(preOrder,preStart+nums_in_left+1,preEnd,inOrder,inRoot+1,inEnd,inorder_Map);
 
    
    return root;
    
    
}

Error image

This is someone's code from discussion form, which is executing.

public TreeNode buildTree(int[] preorder, int[] inorder) {
    Map<Integer, Integer> inMap = new HashMap<Integer, Integer>();

    for(int i = 0; i < inorder.length; i++) {
        inMap.put(inorder[i], i);
    }

    TreeNode root = buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inMap);
    return root;
}

public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inMap) {
    if(preStart > preEnd || inStart > inEnd) return null;

    TreeNode root = new TreeNode(preorder[preStart]);
    int inRoot = inMap.get(root.val);
    int numsLeft = inRoot - inStart;

    root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorder, inStart, inRoot - 1, inMap);
    root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorder, inRoot + 1, inEnd, inMap);

    return root;
}

What am I doing wrong?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

0

The problem is that inorder_map is missing the last value, because this for loop for(int i=0;i<inorder.length-1;i++) should go until be for(int i = 0; i < inorder.length; i++). Because of this, you get a NullPointerException when you will try to access the index of the last value from the inorder array.

On a side note:

I understand that you are solving LeetCode problems, where the quality of your code does not really matter as long as you get the correct result. Please try to adhere to well-known Java Coding conventions.

Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
  • Thank you! That was such a simple mistake from me. Also thank you for sharing the java coding conventions. I will follow it from now on – vishwanath Bhat Jan 29 '22 at 18:31
0

look at the condition where you exist the for loop.

for(int i=0;i<inorder.length-1;i++)
        {
            inorder_map.put(inorder[i],i);
        }

you did not put all the element into the map bacause i<inorder.length-1 will exist when i == inorder.length-1, that means the element where i == inorder.length - 1 did not exiest in the map.

we should change the for loop to:

for(int i = 0;i <= inorder.length - 1; i++)
{
    inorder_map.put(inorder[i], i);
}

or:

for(int i = 0;i < inorder.length; i++)
{
    inorder_map.put(inorder[i], i);
}