0

I'm trying to add some sub lists in a List<List<Integer>> once. But the problem is, it's getting added multiple times.

This is the code:

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if(root == null) return null;
        
        List<List<Integer>> res = new ArrayList<>();
        Queue<Pair> q = new LinkedList<>();
        
        q.add(new Pair(root, 0));
        
        while(!q.isEmpty()) {
          Pair tmp = q.poll();
          int data = tmp.root.val;
          int level = tmp.level;
          
          List<Integer> list;
          // get number of sublists in res
          if(res.size() == level) {
            list = new ArrayList<>();
            list.add(data);
          }
          
          else {
            list = res.get(level);
            System.out.println("existing list = " + list);
            if(level % 2 == 0) {
              list.add(data);
            }
            else list.add(0, data);
          }
          // By now, the sublists are populated
          
          System.out.println("list = " + list);
          // this shows the correct ans
          
          res.add(list);
          // this shows duplicated sublist

          System.out.println("res = " + res);
          
          if(tmp.root.left != null) q.add(new Pair(tmp.root.left, level + 1));
          if(tmp.root.right != null) q.add(new Pair(tmp.root.right, level + 1));
        }
      
      return res;
    }
  
    class Pair {
      TreeNode root;
      int level;
      
      Pair(TreeNode root, int level) {
        this.root = root;
        this.level = level;
      }
    }
}

Please help me on this.

Thanks in advance.

P.S: I think it's a silly mistake somwehere or a blunder.

3
  • This looks like a question that might be best answered by stepping through your code with your debugger to see what is being added where. What happens when you do this? Commented Jun 4, 2021 at 10:46
  • Can you explain what that else part is doing. Commented Jun 4, 2021 at 10:54
  • In that else, I'm trying to get existing list and add elements basis of level. Commented Jun 4, 2021 at 10:55

1 Answer 1

1

This is happening because you're adding the list again and again. You need to add the list in your resulting list i.e res only when there is no list present in res corresponding to that particular level.

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        if(root == null) return new ArrayList<>(); // you don't need to return null, return empty list.
        
        List<List<Integer>> res = new ArrayList<>();
        Queue<Pair> q = new LinkedList<>();
        
        q.add(new Pair(root, 0));
        
        while(!q.isEmpty()) {

          int n = q.size();

          for(int i = 0 ; i  < n ; i++){

            Pair tmp = q.poll();
            int data = tmp.root.val;
            int level = tmp.level;
            
            List<Integer> list;
            // get number of sublists in res
            if(res.size() == level) {
              list = new ArrayList<>();
              list.add(data);
              res.add(list); // You need to add list into res only when there is no list corresponding to that level.
            }
            
            else {
              list = res.get(level);
              // System.out.println("existing list = " + list);
              if(level % 2 == 0) {
                list.add(data);
              }
              else list.add(0, data);
            }

            
            if(tmp.root.left != null) q.add(new Pair(tmp.root.left, level + 1));
            if(tmp.root.right != null) q.add(new Pair(tmp.root.right, level + 1));
          }
        }
        return res;
    }
}
    
class Pair {
  TreeNode root;
  int level;

  Pair(TreeNode root, int level) {
    this.root = root;
    this.level = level;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

if(res.get(level) == null) res.add(list). Are you suggesting this?
I've made the changes in above mentioned answer. Kindly check and let me know if you need any more clarification.
It works! Thanks a lot @Parundeep. It was a silly mistake on my end. Thanks for the help once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.