educative.io

Expected and actual output are same but still getting error

  class Solution {
  public static List<List<Integer>> verticalOrder(TreeNode root) {
ArrayList<List<Integer>> output = new ArrayList<List<Integer>>();;
if(root==null) {
  return output;
}
Queue<Map.Entry<TreeNode, Integer>> queue = new ArrayDeque<>();
queue.offer(new AbstractMap.SimpleEntry(root,0));
HashMap<Integer, List<Integer>> columnMap = new HashMap<Integer, List<Integer>>();
int minColumn = 0;
int maxColumn =0;
while(!queue.isEmpty()) {
  Map.Entry<TreeNode, Integer> queueMap = queue.poll();
  TreeNode node = queueMap.getKey();
  Integer column = queueMap.getValue();
  if(!columnMap.containsKey(column)) {
columnMap.put(column, new ArrayList<Integer>());
  }
  columnMap.get(column).add(node.val);
  minColumn = Math.min(minColumn, column);
  maxColumn = Math.max(maxColumn, column);
  if(node.left!=null) queue.offer(new AbstractMap.SimpleEntry(node.left,column-1));
  if(node.right!=null) queue.offer(new AbstractMap.SimpleEntry(node.right,column+1));
}
for(int i=minColumn;i<maxColumn+1;i++) {
  output.add(columnMap.get(i));
}
return output;
  }
}

Type your question above this line.

Course: https://www.educative.io/collection/10370001/4938268330688512
Lesson: https://www.educative.io/collection/page/10370001/4938268330688512/4798509859995648

1 Like

Yes, faced the same issue and finally after 1hr 45 mins of efforts to change the logic, submitted the code as it is. All got accepted