Get the number of leaf nodes in a binary tree non-recursively

The node with empty left child node and right child node is the leaf node

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;

}

Guess you like

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