• 2021-04-14
    1.编写递归算法,将二叉树中所有结点的左、右子树相互交换。StatusExchangeBiTree(BiTree&T){BiTreep;if(T){p=T->lchild;T->lchild=T->rchild;T->rchild=p;ExchangeBiTree(T->lchild);}returnOK;}
  • ExchangeBiTree(T->rchild);

    举一反三

    内容

    • 0

      以下程序段采用先根遍历方法求二叉树的叶子数,请在横线处填充适当的语句(注意:字符之间不要留空白)。 void countleaf (bitree ptr t,int &count) { /*根指针为t,假定叶子数 count 的初值 = 0 */ if (t!=NULL) { if ( (t->lchild==NULL) && (t->rchild==NULL)) (1)________; countleaf (t->lchild,count); countleaf(l->rchild,count); } }

    • 1

      1.编写递归算法,将二叉树中所有结点的左、右子树相互交换。StatusExchangeBiTree(BiTreeT){BiTreep;if(T){p=T-lchild;T-lchild=T-rchild;T-rchild=p;ExchangeBiTree(T-lchild);}returnOK;}

    • 2

      一棵二叉树采用二叉链表存储结构存储,根指针为t,下列递归算法求其先序序列中第k(1≦k≦二叉树中结点的个数)个结点的值,算法的画线处应填的语句是 。[img=369x222]1802fcd73a99dd0.jpg[/img] A: k-- B: n++ C: t = t->lchild D: t = t->rchild

    • 3

      设t是给定的一棵二叉树,下面的递归程序count(t)用于求得:二叉树t中具有非空的左,右两个孩子的结点个数N2;只有非空左孩子的个数NL;只有非空右孩子的结点个数NR和叶子结点个数N0。其中N2、NL、NR、N0都是全局变量,且在调用count(t)之前都置为0。请将下面程序空处补完整。 typedef struct node { int data; Struct node *lchild,*rchild; }node; int N2,NL,NR,N0; void count(node *t) { if (t->lchild!=NULL) if ( (1)__ _) N2++; else NL++; else if ( (2)_ __) NR++; else (3)_ __; if(t->lchild!=NULL) (4)__ _; if(t->rchild!=NULL) (5)__ _; }

    • 4

      写出下面算法的功能。Bitree*function(Bitree*bt){Bitree*t,*t1,*t2;if(bt==NULL)t=NULL;else{t=(Bitree*)malloc(sizeof(Bitree));t->data=bt->data;t1=function(bt->left);t2=function(bt->right);t->left=t2;t->right=t1;}return(t);}