Get Run-time Error?? Can someone point me the mistakes in my code ?


  • 0
    Y
        ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
        if(root == null) return results;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int curLevel = 1;
        int nextLevel = 0;
        ArrayList<Integer> result = new ArrayList<Integer>();
        boolean right_to_left = true;
        while(!queue.isEmpty()) {
        	TreeNode cur = queue.poll();
        	result.add(cur.val);
        	curLevel--;
        	if(right_to_left){
        	if(cur.right != null) {
        		queue.offer(cur.left);
        		nextLevel++;
        	}
        	if(cur.left != null) {
        		queue.offer(cur.right);
        		nextLevel++;
        	}
        	}
        	else{
        	    if(cur.left != null) {
        		queue.offer(cur.left);
        		nextLevel++;
        	  }
        	if(cur.right != null) {
        		queue.offer(cur.right);
        		nextLevel++;
        	  }
        	}
        	if(curLevel == 0) {
        		results.add(result);
        		right_to_left = !right_to_left;
        		curLevel = nextLevel;
        		nextLevel = 0;
        		if(curLevel != 0) result = new ArrayList<Integer>();
        	}
        }
        return results;

Log in to reply
 

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