Obtenga la cantidad de nodos hoja en un árbol binario de forma no recursiva

El nodo con el nodo hijo izquierdo vacío y el nodo hijo derecho es el nodo hoja

int numberOfLeavesInBTusingLevelOrder(BinaryTreeNode  root){
    
    
  BinaryTreeNode temp;
  LLQueue q = new LLQueue();
  int count = 0;
  if(root == null){
    
    
    return 0;
  }
  q.enQueue(root);
  while(!q.isEmpty()){
    
    
    temp = q.deQueue();
    if(temp.getLeft() == null && temp.getRight() == null)
     count++;
    else{
    
    
      if(temp.getLeft() != null){
    
    
        q.enQueue(temp.getLeft());
      }
      if(temp.getRight()){
    
    
        q.enQueue(temp.getRight());
      }
    }
  }
  q.deleteQueue();
  return count;

}

Supongo que te gusta

Origin blog.csdn.net/weixin_37632716/article/details/110939836
Recomendado
Clasificación