结构推理 以二叉链表为存储结构,写一算法交换各结点的左右子树。
【正确答案】思想:借助栈来进行对换。
   Swap(BinTree*T)
   {
     BinTree *stack[100],*temp;
     int top=-1;
     root=T;
     if(T!=Null)
   }
     top++;
     stack[top]=T;
     while(top>-1)
     {
      T=stack[top];
      top--;
      if(T->child!=Null||T->rchild!=Null)
      {    /*交换结点的左右指针*/
       temp=T->lchild;
       T->lchild=T->rchild;
       T->rchild=temp;
      }
     if(T->lchild!=Null)
      {
      top++;
      stack[top]=T->lchild;
     }
       if(T->rchild!=Null)
        {
         top++;
         stack[top]=T->rchild
         }
       }
     }    /*endwhile*/
   }    /*endif*/
   main()
   {
    int i,j,k,l;
    printf("\n");
    root=-CreateBinTree();
    Inorder(root);
    i=CountNode(root);
    j=CountLeafs(root);
    k=Depth(root);
    l=Width(root);
    printf("\nThe Node's Number:/%d",i);
    printf("\nThe Leafs's Number:/%d",j);
    printf("\nThe Depth is:/%d",k);
    printf("\nThe width is:/%d",1);
    Swap(root);
    Pfintf("\nThe swapTree is:");
    Inorder(root);
    }
【答案解析】