添加兄弟节点

带有三个指针(左指针,右指针和兄弟指针)的二叉树,设计填充下一个兄弟指针

void FillNextSiblings(BinaryTreeNode root){
    
    
  llQueue q = new LLQueue();
  BinaryNode temp;
  if(root == null)
    return;
  q.enQueue(root);
  q.enQueue(null);
  while(q.isNotEmpty()){
    
    
    temp = q.deQueue();
    if(temp == null){
    
    
      q.enQueue(null);
    }else{
    
    
      temp.setNextSiblings(q.pop());
      if(temp.getLeft() != null){
    
    
        q.enQueue(temp.getLeft());
      }
      if(temp.getRight() != null){
    
    
        q.enQueue(temp.getRight());
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_37632716/article/details/114899530