Non-recursive algorithm to find half nodes in a binary tree

A half node is a node with only the left child or the right child node

int numberOfHalfNodesINBTusingLevelOrder(BinaryTreeNode root){
    
    
  BinaryTreeNode temp;
  llQueue q = new llQueue();
  int count = 0;
  if(root == null)
    return count;
  while(!q.isEmpty()){
    
    
    temp = q.deQueue();
    if(temp.getleft() != null && temp.getRight() == null || temp.getLeft() == null && temp.getRight() n != null){
    
    
    count++;
  }
  if(temp.getleft()!=null){
    
    
    q.enQueue(temp.getLeft());
  }
  if(){
    
    
    q.enQueue(temp.getRight());
  }
  }
  q.deleteQueue();
  return coount;
}

Guess you like

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