递归:
void exchange(BTree *rt){
 BTree *temp = NULL;
 if(rt->lchild == NULL && rt->rchild == NULL)
        return;
 else{
       temp = rt->lchild;
       rt->lchild = rt->rchild;
       rt->rchild = temp;
 }
 if(rt->lchild)
      exchange(rt->lchild);
 if(rt->rchild)
      exchange(rt->rchild);
}

非递归:
Stack是一个定义好的通用堆栈类型,支持初始化/进站/出战等操作
int Exchange(BiTree *T)
{
  Stack s;
  BiTree *p;
  InitStack(s);
  p=T;
  while(p||!StackEmpty(s)){
    if(p){
      push(s,p);
      p->Lchild;
    }
    else{
      pop(s,p);
      t=p->Rchild;
      p->Rchild=p->Lchild;
      p->Lchild=t;
      p-=->Lchild;
    }
  }
return 1;
}

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-01-17
  • 2021-06-04
  • 2021-11-24
  • 2022-12-23
相关资源
相似解决方案