TLE & weird Compile Error


  • 0

    Hi, bellow you can see my code.

    I get TLE for the simplest use case (input: [], []) which is obviously handled separately at the top of buildTree method.

    A couple of times, after submitting my solution, I also got the following error:

    Compile Error
    Line 9: error: cannot find symbol: class FileWriter

    public TreeNode buildTree(int [] inorder, int [] postorder) {
        
        if (inorder.length == 0 || postorder.length == 0)
            return null;
        
    	LinkedList<Integer> inorderList = new LinkedList<Integer>();
    	for (int i = 0; i < inorder.length; i++)
    		inorderList.add(inorder[i]);
    	
    	LinkedList<Integer> postorderList = new LinkedList<Integer>();
    	for (int i = 0; i < postorder.length; i++)
    		postorderList.add(postorder[i]);
    	
    	return buildSubTree(inorderList, postorderList);
    	
    }
    
    private TreeNode buildSubTree(List<Integer> inorder, List<Integer> postorder) {
        
        if (inorder.size() == 0 || postorder.size() == 0)
            return null;
        
        int root = postorder.get(postorder.size()-1);
        TreeNode node = new TreeNode(root);
        
        int idx = inorder.indexOf(root);
        
        List<Integer> inorderLeft = inorder.subList(0, idx);
        List<Integer> postorderLeft = postorder.subList(0, idx);
        node.left = buildSubTree(inorderLeft, postorderLeft);
        
        List<Integer> inorderRight =  inorder.subList(idx+1, inorder.size());
        List<Integer> postorderRight = postorder.subList(idx, postorder.size()-1);
    	node.right = buildSubTree(inorderRight, postorderRight);
    	
        
        return node;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.