Java-将树转换为具有相同深度的节点列表

Java-将树转换为具有相同深度的节点列表,java,binary-tree,Java,Binary Tree,我正在尝试编写一个代码,将二叉树转换为具有相同深度的节点列表。如果树具有深度d,则将创建深度d列表。逻辑是按顺序遍历,并将当前遍历的节点添加到适当深度的列表中 public void treeToListofNodesByLevel(Node<T> n,int depth, ArrayList<LinkedList<Node<T>>> treeList){ if(n.right != null){ inOr

我正在尝试编写一个代码,将二叉树转换为具有相同深度的节点列表。如果树具有深度d,则将创建深度d列表。逻辑是按顺序遍历,并将当前遍历的节点添加到适当深度的列表中

public void treeToListofNodesByLevel(Node<T> n,int depth, ArrayList<LinkedList<Node<T>>> treeList){

        if(n.right != null){
            inOrderWithHeight(n.right, depth + 1);
        }
        if(treeList.size() >= depth){
            treeList.add(depth, new LinkedList<Node<T>>() );
        }
        treeList.get(depth).add(n);
        if(n.left != null){
            inOrderWithHeight(n.left, depth + 1);
        }

    }
节点级别的公共void树列表(节点n,int-depth,ArrayList树列表){
如果(n.right!=null){
按高度顺序排列(n.右侧,深度+1);
}
if(treeList.size()>=深度){
add(depth,newlinkedlist());
}
树列表.get(深度).add(n);
如果(n.left!=null){
顺序为高度(n.左侧,深度+1);
}
}
然后打电话:

ArrayList<LinkedList<Node<T>>> result = new ArrayList<LinkedList<Node<T>>>();
treeToListofNodesByLevel(root, 0, result);
ArrayList结果=新建ArrayList();
节点级别的树列表(根,0,结果);
这样行吗?有我不处理的角落案件吗?
另外,现在我正在传递该方法返回的列表,因为我想不出一种方法来初始化它,然后在结束时返回它,同时保持递归结构。有更好的方法吗?

我无法理解“inOrderWithHeight”方法在做什么。对于这个问题(未优化)我所做的是使用两个队列像BFS一样遍历,并且对于每个迭代,将该深度的节点添加到该迭代的列表中(每个迭代遍历树的一个深度)。下面是我为二叉树编写的代码,正如您在回答中所设想的:

Queue<Node<T>> queue1 = new LinkedList<Node<T>>() ;
Queue<Node<T>> queue2 = new LinkedList<Node<T>>() ;
public void treeToListofNodesByLevel(Node<T> root, ArrayList<LinkedList<Node<T>>> treeList) {
    if (root == null)
        return;
    int curr_depth = 0;
    queue1.add(root);
    while(!queue1.isEmpty()){
        treeList.add(curr_depth, new LinkedList<Node<T>>());
        while(!queue1.isEmpty()){
            Node<T> node = queue1.remove();
            treeList.get(curr_depth).add(node);
            if(node.left != null) queue2.add(node.left);
            if(node.right != null) queue2.add(node.right);
        }
        curr_depth++;
        while(!queue2.isEmpty()){
            Node<T> node = queue2.remove();
            queue1.add(node);
        }    
    }
}
Queue queue1=new LinkedList();
Queue queue2=新的LinkedList();
NodeByLevel(节点根、ArrayList树列表)的公共无效树列表{
if(root==null)
返回;
int curr_depth=0;
queue1.add(root);
而(!queue1.isEmpty()){
添加(curr_depth,newlinkedList());
而(!queue1.isEmpty()){
Node Node=queue1.remove();
treeList.get(curr\u depth).add(node);
如果(node.left!=null)queue2.add(node.left);
如果(node.right!=null)queue2.add(node.right);
}
电流深度++;
而(!queue2.isEmpty()){
Node Node=queue2.remove();
queue1.add(节点);
}    
}
}

我在没有语法检查的情况下动态编写了这篇文章,可能有编译器错误。但希望你能理解。

我无法理解“有序高度”方法在做什么。对于这个问题(未优化)我所做的是使用两个队列像BFS一样遍历,并且对于每个迭代,将该深度的节点添加到该迭代的列表中(每个迭代遍历树的一个深度)。下面是我为二叉树编写的代码,正如您在回答中所设想的:

Queue<Node<T>> queue1 = new LinkedList<Node<T>>() ;
Queue<Node<T>> queue2 = new LinkedList<Node<T>>() ;
public void treeToListofNodesByLevel(Node<T> root, ArrayList<LinkedList<Node<T>>> treeList) {
    if (root == null)
        return;
    int curr_depth = 0;
    queue1.add(root);
    while(!queue1.isEmpty()){
        treeList.add(curr_depth, new LinkedList<Node<T>>());
        while(!queue1.isEmpty()){
            Node<T> node = queue1.remove();
            treeList.get(curr_depth).add(node);
            if(node.left != null) queue2.add(node.left);
            if(node.right != null) queue2.add(node.right);
        }
        curr_depth++;
        while(!queue2.isEmpty()){
            Node<T> node = queue2.remove();
            queue1.add(node);
        }    
    }
}
Queue queue1=new LinkedList();
Queue queue2=新的LinkedList();
NodeByLevel(节点根、ArrayList树列表)的公共无效树列表{
if(root==null)
返回;
int curr_depth=0;
queue1.add(root);
而(!queue1.isEmpty()){
添加(curr_depth,newlinkedList());
而(!queue1.isEmpty()){
Node Node=queue1.remove();
treeList.get(curr\u depth).add(node);
如果(node.left!=null)queue2.add(node.left);
如果(node.right!=null)queue2.add(node.right);
}
电流深度++;
而(!queue2.isEmpty()){
Node Node=queue2.remove();
queue1.add(节点);
}    
}
}

我在没有语法检查的情况下动态编写了这篇文章,可能有编译器错误。但希望你能明白这个想法。

你的总体概念非常完美。它将起作用,并应处理所有案件

但是,您在详细信息中有一些错误:


  • 检查何时添加新列表时,比较方向错误。它应该是
    if(treeList.size()您的一般概念非常完美。它可以工作,并且应该处理所有情况

    但是,您在详细信息中有一些错误:


    • 检查何时添加新列表时,比较方向错误。应该是
      if(treeList.size()为什么只使用相同的函数代码会更漂亮:

          public void treeToListofNodesByLevel(BinaryTree node, int currentDepth, List<List<BinaryTree>> result){
      
          // Check if the list for the currentDepth is created
          if(result.get(currentDepth) != null){
              result.add(currentDepth, new ArrayList<BinaryTree>())
          }
      
          // Add the current node to the list
          result.get(currentDepth).add(node);
      
      
          // Recursive the left node
          if(node.left != null){
              treeToListofNodesByLevel(node.left, currentDepth+1, result);
          } 
      
          // Recursive the right node
          if(node.right != null){
              treeToListofNodesByLevel(node.right, currentDepth+1, result);
          }
      }
      
      public void treetolistOfNodeByLevel(二进制树节点,int currentDepth,列表结果){
      //检查是否创建了currentDepth的列表
      if(result.get(currentDepth)!=null){
      添加(currentDepth,new ArrayList())
      }
      //将当前节点添加到列表中
      result.get(currentDepth).add(node);
      //递归左节点
      if(node.left!=null){
      NodeByLevel树列表(node.left,currentDepth+1,结果);
      } 
      //递归右节点
      if(node.right!=null){
      NodeByLevel树列表(node.right,currentDepth+1,结果);
      }
      }
      
      在您的主要功能中:

      List<List<BinaryTree>> result = new ArrayList<>();
      BinaryTree root = new BinaryTree();  
      treeToListofNodesByLevel(root, 0, result);
      
      List result=new ArrayList();
      BinaryTree根=新的BinaryTree();
      节点级别的树列表(根,0,结果);
      
      为什么只使用相同的功能代码会更美观:

          public void treeToListofNodesByLevel(BinaryTree node, int currentDepth, List<List<BinaryTree>> result){
      
          // Check if the list for the currentDepth is created
          if(result.get(currentDepth) != null){
              result.add(currentDepth, new ArrayList<BinaryTree>())
          }
      
          // Add the current node to the list
          result.get(currentDepth).add(node);
      
      
          // Recursive the left node
          if(node.left != null){
              treeToListofNodesByLevel(node.left, currentDepth+1, result);
          } 
      
          // Recursive the right node
          if(node.right != null){
              treeToListofNodesByLevel(node.right, currentDepth+1, result);
          }
      }
      
      public void treetolistOfNodeByLevel(二进制树节点,int currentDepth,列表结果){
      //检查是否创建了currentDepth的列表
      if(result.get(currentDepth)!=null){
      添加(currentDepth,new ArrayList())
      }
      //将当前节点添加到列表中
      result.get(currentDepth).add(node);
      //递归左节点
      if(node.left!=null){
      NodeByLevel树列表(node.left,currentDepth+1,结果);
      } 
      //递归右节点
      if(node.right!=null){
      NodeByLevel树列表(node.right,currentDepth+1,结果);
      }
      }
      
      在您的主要功能中:

      List<List<BinaryTree>> result = new ArrayList<>();
      BinaryTree root = new BinaryTree();  
      treeToListofNodesByLevel(root, 0, result);
      
      List result=new ArrayList();
      BinaryTree根=新的BinaryTree();
      节点级别的树列表(根,0,结果);
      
      高度为4的完整二叉树将有8个不同的可能路径。在这种情况下,列表会是什么样子?高度为4 w的完整二叉树