Java-在列表中多次添加值

Java-在列表中多次添加值,java,binary-tree,breadth-first-search,Java,Binary Tree,Breadth First Search,我试图在列表中添加一些子列表。但问题是,它被多次添加 代码如下: class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { if(root == null) return null; List<List<Integer>> res = new ArrayList<>();

我试图在
列表中添加一些子列表。但问题是,它被多次添加

代码如下:

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;
      }
    }
}
类解决方案{
公共列表zigzagLevelOrder(TreeNode根目录){
if(root==null)返回null;
List res=new ArrayList();
队列q=新的LinkedList();
q、 添加(新对(根,0));
而(!q.isEmpty()){
配对tmp=q.poll();
int data=tmp.root.val;
int level=tmp.level;
名单;
//获取res中的子列表数
如果(分辨率大小()=级别){
列表=新的ArrayList();
列表。添加(数据);
}
否则{
列表=res.get(级别);
System.out.println(“现有列表=”+列表);
如果(级别%2==0){
列表。添加(数据);
}
其他列表。添加(0,数据);
}
//现在,子列表已填充
System.out.println(“list=“+list”);
//这显示了正确的ans
决议添加(列表);
//这将显示重复的子列表
System.out.println(“res=“+res”);
如果(tmp.root.left!=null)q.add(新对(tmp.root.left,级别+1));
如果(tmp.root.right!=null)q.add(新对(tmp.root.right,级别+1));
}
返回res;
}
类对{
树根;
智力水平;
成对(树节点根,整数级){
this.root=根;
这个水平=水平;
}
}
}
请帮我做这个

提前谢谢


附言:我认为这是一个愚蠢的错误,或者是一个错误。

发生这种情况是因为你一次又一次地添加列表。您需要将列表添加到结果列表中,即仅当对应于该特定级别的res中没有列表时才添加res

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;
  }
}
类解决方案{
公共列表zigzagLevelOrder(TreeNode根目录){
如果(root==null)返回新的ArrayList();//您不需要返回null,请返回空列表。
List res=new ArrayList();
队列q=新的LinkedList();
q、 添加(新对(根,0));
而(!q.isEmpty()){
int n=q.size();
对于(int i=0;i
这看起来像是一个问题,最好通过使用调试器单步检查代码,看看在哪里添加了什么来回答这个问题。当你这样做时会发生什么?你能解释一下其他部分在做什么吗?在其他部分中,我试图获取现有列表,并在级别的基础上添加元素。if(res.get(level)==null)res.add(list)。你是在暗示这个吗?我已经在上面提到的答案中做了修改。请检查,让我知道如果你需要任何更多的澄清。它的工作!非常感谢@Parundeep。在我这方面,这是一个愚蠢的错误。再次感谢你的帮助。