Output all paths from the root node to the leaf node

public void printPaths(){
    
    
  int[] path = new int[256];
  printPaths(node,path,0);
}

private void printPaths(BinaryTreeNode node,int[] path,int pathlen){
    
    
  if(node == null) return;
  //将根节点添加到赎罪当中
  path[pathlen] = node.getData();
  pathlen++;
  //如果当前节点为叶子节点,则输出到此节点的路径
  if(node.getleft() == null && node.getRight() == null){
    
    
    printArray(path,pathlen);
  }
  else{
    
    
    //否则继续遍历左右子树
    printPaths(node.getLeft(),path,pathlen);
    printPaths(node.getRight().path,pathlen);
  }
}

private void printArray(int[] ints,int len){
    
    
  for(int i = 0; i<len; i++){
    
    
    System.out.print(ints[i]+"");
  }
  System.out.println();
}

Guess you like

Origin blog.csdn.net/weixin_37632716/article/details/114334592