비재 귀적으로 이진 트리의 리프 노드 수 가져 오기

왼쪽 자식 노드가 비어 있고 오른쪽 자식 노드가있는 노드가 리프 노드입니다.

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;

}

추천

출처blog.csdn.net/weixin_37632716/article/details/110939836