PTA 2023-2024-1-数据结构练习题重现(递归+二叉树)

6-16 使用递归函数计算1到n之和

本题要求实现一个用递归计算1+2+3+…+n的和的简单函数。

函数接口定义:

int sum( int n );

该函数对于传入的正整数n返回1+2+3+…+n的和;若n不是正整数则返回0。题目保证输入输出在长整型范围内。建议尝试写成递归函数。

裁判测试程序样例:

#include <stdio.h>

int sum( int n );

int main()
{
    int n;

    scanf("%d", &n);
    printf ("%d\n", sum(n));

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

10

输出样例1: 

55

输入样例2: 

0

输出样例2: 

0

代码示例:

int sum( int n ){
    if(n<1) return 0;
    if(n==1) return 1;
    return sum(n-1)+n;
}

 6-17 递归求Fabonacci数列

本题要求实现求Fabonacci数列项的函数。Fabonacci数列的定义如下:

f(n)=f(n−2)+f(n−1) (n≥2),其中f(0)=0,f(1)=1。

函数接口定义:

int f( int n );

函数f应返回第n个Fabonacci数。题目保证输入输出在长整型范围内。建议用递归实现。

裁判测试程序样例:

#include <stdio.h>

int f( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    printf("%d\n", f(n));
    
    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

6

输出样例: 

8

代码示例: 

int f( int n ){
    if(n==0) return 0;
    if(n==1) return 1;
    return f(n-2)+f(n-1);
}

6-18 递归实现顺序输出整数

本题要求实现一个函数,对一个整数进行按位顺序输出。

函数接口定义:

void printdigits( int n );

函数printdigits应将n的每一位数字从高位到低位顺序打印出来,每位数字占一行。

裁判测试程序样例:

#include <stdio.h>

void printdigits( int n );

int main()
{
    int n;
    
    scanf("%d", &n);
    printdigits(n);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

12345

输出样例: 

1

2

3

4

5

代码示例: 

void printdigits( int n ){
    if(n<10){
        printf("%d\n",n);
        return;
    }
    printdigits(n/10);
    printf("%d\n",n%10);
}

6-19 单链表逆序输出

本题要求实现一个函数,逆序输出不带头结点的单链表的的所有元素值。

函数接口定义:

void Reverse ( LinkList L );

其中LinkList结构定义如下:

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

L是不带头结点的单链表的头指针,即单链表首元素结点指针。

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

LinkList Create ();/* 细节在此不表 */
void Reverse ( LinkList L );
int main()
{
    LinkList L = Create();
    Reverse(L);
    return 0;
}


/* 请在这里填写答案 */

输入样例:

输入是以-1结束的整数序列,-1不属于单链表元素,整数之间空格分隔。

2 1 4 5 3 -1

输出样例:

逆序输出单链表所有元素,格式为每个整数后面跟一个空格。

3 5 4 1 2

 代码示例:

void Reverse ( LinkList L ){
    if(!L) return;
    Reverse(L->next);
    printf("%d ",L->data);
}

6-20 递归求单链表最大值

本题要求实现一个函数,返回不带头结点的单链表中最大元素的地址。

函数接口定义:

LinkList MaxP( LinkList L);

L是不带头结点的单链表的头指针,函数MaxP返回表中最大元素的地址。如果单链表为空,返回空指针。

其中LinkList结构定义如下:

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;

LinkList Create();/* 细节在此不表 */

LinkList MaxP( LinkList L);

int main()
{
    LinkList L,p;
    ElemType e;
    L = Create();
    p = MaxP(L);
    if(p)
        printf("%d\n", p->data);
    else
        printf("NULL");
    return 0;
}

/* 你的代码将被嵌在这里 */

输入格式:

输入数据为1行,给出以-1结束的单链表元素(-1不属于单链表元素),所有数据之间用空格分隔。

输入样例:

2 5 4 5 3 -1

输出样例: 

5

代码示例: 

LinkList MaxP( LinkList L){
    if(!L||!L->next) return L;
    LinkList maxp = MaxP(L->next);
    if(maxp->data>L->data) return maxp;
    else return L;
}

6-21 青蛙跳台阶

一只青蛙一次可以跳上1级台阶、2级台阶、3级台阶。求这只青蛙跳上一个n(0<=n<=20)级台阶总共有多少种跳法(先后次序不同算不同的结果)。

函数接口定义:

int climb ( int n );

n为台阶数,函数须返回 n级台阶的跳法总数。

裁判测试程序样例:

#include <stdio.h>
int climb(int n);
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d\n", climb(n));
    return 0;
}

/* 请在这里填写答案 */

输入样例:

5

输出样例: 

13

代码示例:

int climb ( int n ){
    if(n==0) return 1;
    if(n==1) return 1;
    if(n==2) return 2;
    if(n==3) return 4;
    return climb(n-1)+climb(n-2)+climb(n-3);
}

6-22 统计二叉树结点个数

本题要求实现一个函数,可统计二叉树的结点个数。

函数接口定义:

int NodeCount ( BiTree T);

T是二叉树树根指针,函数NodeCount返回二叉树中结点个数,若树为空,返回0。

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 细节在此不表 */

int NodeCount ( BiTree T);

int main()
{
    BiTree T = Create();
    
    printf("%d\n", NodeCount(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C##

输出样例(对于图中给出的树): 

6

代码示例: 

int NodeCount ( BiTree T){
    if(!T) return 0;
    else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}

6-23 统计二叉树叶子结点个数

本题要求实现一个函数,可统计二叉树的叶子结点个数。

函数接口定义:

int LeafCount ( BiTree T);

T是二叉树树根指针,函数LeafCount返回二叉树中叶子结点个数,若树为空,则返回0。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 细节在此不表 */

int LeafCount ( BiTree T);

int main()
{
    BiTree T = Create();
    
    printf("%d\n", LeafCount(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C## 

输出样例(对于图中给出的树): 

3

6-24 统计二叉树度为1的结点个数 

本题要求实现一个函数,可统计二叉树中度为1的结点个数。

函数接口定义:

int NodeCount ( BiTree T);

T是二叉树树根指针,函数NodeCount返回二叉树中度为1的结点个数,若树为空,返回0。

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 细节在此不表 */

int NodeCount ( BiTree T);

int main()
{
    BiTree T = Create();
    
    printf("%d\n", NodeCount(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C##

输出样例(对于图中给出的树):

 

1

 代码示例:

int NodeCount ( BiTree T){
    if(!T) return 0;
    if((!T->lchild&&T->rchild)||(T->lchild&&!T->rchild)) 
        return 1+NodeCount(T->lchild)+NodeCount(T->rchild);
    return NodeCount(T->lchild)+NodeCount(T->rchild);
}

6-25 统计二叉树度为2的结点个数

本题要求实现一个函数,可统计二叉树中度为2的结点个数。

函数接口定义:

int NodeCount ( BiTree T);

T是二叉树树根指针,函数NodeCount返回二叉树中度为2的结点个数,若树为空,返回0。

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

BiTree Create();/* 细节在此不表 */

int NodeCount ( BiTree T);

int main()
{
    BiTree T = Create();
    
    printf("%d\n", NodeCount(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C##

输出样例(对于图中给出的树): 

2

代码示例: 

int NodeCount ( BiTree T){
    if(!T) return 0;
    if(T->lchild&&T->rchild)
        return 1+NodeCount(T->lchild)+NodeCount(T->rchild);
    return NodeCount(T->rchild)+NodeCount(T->lchild);
}

6-26 求二叉树的深度

本题要求实现一个函数,可返回二叉树的深度。

函数接口定义:

int Depth(BiTree T);

T是二叉树树根指针,函数Depth返回二叉树的深度,若树为空,返回0。

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

int Depth(BiTree T);

int main()
{
    BiTree T = Create();

    printf("%d\n", Depth(T));
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C##

输出样例(对于图中给出的树):

 4

代码示例: 

int Depth(BiTree T){
    if(!T) return 0;
    else{
        int l = Depth(T->lchild);
        int r = Depth(T->rchild);
        if(l>r) return l+1;
        else return r+1;
    }
}

6-27 先序输出叶结点 

本题要求按照先序遍历的顺序输出给定二叉树的叶结点。

函数接口定义:

void PreorderPrintLeaves( BinTree BT );

其中BinTree结构定义如下:

typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

函数PreorderPrintLeaves应按照先序遍历的顺序输出给定二叉树BT的叶结点,格式为一个空格跟着一个字符。

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

BinTree CreatBinTree(); /* 实现细节忽略 */
void PreorderPrintLeaves( BinTree BT );

int main()
{
    BinTree BT = CreatBinTree();
    printf("Leaf nodes are:");
    PreorderPrintLeaves(BT);
    printf("\n");

    return 0;
}
/* 你的代码将被嵌在这里 */

输出样例(对于图中给出的树):

Leaf nodes are: D E H I

代码示例: 

void PreorderPrintLeaves( BinTree BT ){
    if(BT){
        if(!BT->Left&&!BT->Right)
            printf(" %c",BT->Data);
        PreorderPrintLeaves(BT->Left);
        PreorderPrintLeaves(BT->Right);
    }
}

6-28 二叉树的三种遍历(先序、中序和后序)

本题要求实现给定的二叉树的三种遍历。

函数接口定义:

void Preorder(BiTree T);
void Inorder(BiTree T);
void Postorder(BiTree T);

T是二叉树树根指针,Preorder、Inorder和Postorder分别输出给定二叉树的先序、中序和后序遍历序列,格式为一个空格跟着一个字符。

其中BinTree结构定义如下:

typedef char ElemType;
typedef struct BiTNode
{
   ElemType data;
   struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
   ElemType data;
   struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

void Preorder(BiTree T);
void Inorder(BiTree T);
void Postorder(BiTree T);

int main()
{
   BiTree T = Create();
   printf("Preorder:");   Preorder(T);   printf("\n");
   printf("Inorder:");    Inorder(T);    printf("\n");
   printf("Postorder:");  Postorder(T);  printf("\n");
   return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C## 

输出样例(对于图中给出的树): 

Preorder: A B D F G C
Inorder: B F D G A C
Postorder: F G D B C A

代码示例: 

void Preorder(BiTree T){
    if(T){
        printf(" %c",T->data);
        Preorder(T->lchild);
        Preorder(T->rchild);
    }
    else return;
}
void Inorder(BiTree T){
    if(!T) return;
    Inorder(T->lchild);
    printf(" %c",T->data);
    Inorder(T->rchild);
}
void Postorder(BiTree T){
    if(!T) return;
    Postorder(T->lchild);
    Postorder(T->rchild);
    printf(" %c",T->data);
}

6-13 后缀表达式

本题要求实现一个函数,输出二叉树表示的表达式的后缀式。

函数接口定义:

void Suffix(BiTree T);

T是表达式二叉树树根指针,函数Infix输出该表达式的后缀式,格式为一个字符后面跟着一个空格。

裁判测试程序样例:


typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */
void Suffix(BiTree T);

int main()
{
    BiTree T = Create();
    Suffix(T);
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例

对于图中给出的二叉树,输入数据为扩展的先序序列:

+-A##*F##G##C##

输出样例(对于图中给出的树): 

A F G * - C + 

代码示例: 

void Suffix(BiTree T){
    if(!T) return;
    Suffix(T->lchild);
    Suffix(T->rchild);
    printf("%c ",T->data);
}

6-30 二叉树的层次遍历

本题要求实现给定的二叉树的层次遍历。

函数接口定义:

void Levelorder(BiTree T);

T是二叉树树根指针,Levelorder函数输出给定二叉树的层次遍历序列,格式为一个空格跟着一个字符。

其中BinTree结构定义如下:

typedef char ElemType;
typedef struct BiTNode
{
   ElemType data;
   struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
   ElemType data;
   struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

void Levelorder(BiTree T);

int main()
{
   BiTree T = Create();
   printf("Levelorder:"); Levelorder(T); printf("\n");
   return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。例如对于如下二叉树,输入数据:

AB#DF##G##C##

输出样例(对于图中给出的树): 

 

Levelorder: A B C D F G

代码示例: 

void Levelorder(BiTree T){
    BiTree q[1000];
    int front=0,rear=0;
    if(T) q[rear++]=T;
    while(front!=rear)
    {
        BiTree p=q[front++];//出队的同时左右孩子入队
        printf(" %c",p->data);
        if(p->lchild) q[rear++]=p->lchild;//左右孩子入队
        if(p->rchild) q[rear++]=p->rchild;
    }
}

6-11 打印二叉树指定层次结点

本题要求实现按从左到右顺序打印二叉树的指定层次的结点序列。

函数接口定义:

void PrintLevel(BiTree T,int n);

T是二叉树树根指针,n为指定打印的层次,PrintLevel函数按从左到右顺序输出二叉树的指定层次的结点序列,格式为一个字符后面跟着一个空格。

其中BinTree结构定义如下:

typedef char ElementType;
typedef struct BiTNode {
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode, *BiTree;

裁判测试程序样例:


#include <stdio.h>
#include <stdlib.h>

typedef char ElementType;
typedef struct BiTNode {
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

void PrintLevel(BiTree T, int n);

int main()
{
   int n;
    BiTree T = Create();
    scanf("%d", &n);
    PrintLevel(T, n);
    printf("\n");
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

对于图中给出的二叉树,输入数据为两行,第一行是扩展的先序序列,第二行是要输出的层次数:

AB#DF##G##C##
2

输出样例: 

B C 

代码示例:

void PrintLevel(BiTree T,int n){
    if(!T) return;
    if(n==1) printf("%c ",T->data);
    else{
        PrintLevel(T->lchild,n-1);
        PrintLevel(T->rchild,n-1);
    }
}

 6-14 层次输出第n个结点

本题要求实现对于给定的二叉树,打印层次遍历序列中指定序号的结点。

函数接口定义:

void PrintNode(BiTree T,int n);

T是二叉树树根指针,PrintNode函数输出给定二叉树的层次遍历序列中第n个结点,n为结点在层次遍历序列中的序号,从1开始编号。

其中BinTree结构定义如下:

typedef char ElemType;
typedef struct BiTNode
{
   ElemType data;
   struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>

typedef char ElemType;
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;

BiTree Create();/* 细节在此不表 */

void PrintNode(BiTree T,int n);
int main()
{
    BiTree T = Create();
    int n;
    scanf("%d", &n);
    printf("The %d-th node in levelorder is: ", n);
    PrintNode(T,n);
    printf("\n");
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

输入第一行给出由字母和'#'组成的字符串,代表二叉树的扩展先序序列,第二行给出要打印的结点在后序序列中的序号n。例如对于如下二叉树,输入数据:

AB#DF##G##C##
2

输出样例(对于图中给出的树,当输入的n为2时): 

The 2-th node in levelorder is: B

代码示例: 

void PrintNode(BiTree T,int n){
    BiTree q[1000];
    int front = 0,rear = 0;
    if(T) q[rear++] = T;
    int cnt = 0;
    while(front!=rear){
        BiTree p = q[front++];
        cnt++;
        if(cnt==n){
            printf("%c",p->data);
            return;
        }
        if(p->lchild) q[rear++] = p->lchild;
        if(p->rchild) q[rear++] = p->rchild;
    }
}

6-12 从下往上打印指定元素的所有祖先

本题要求实现按从下往上打印二叉树中指定元素的祖先。

函数接口定义:

int PrintAncestors(BiTree T,char ch);

T是二叉树树根指针,ch为指定的元素值,如果ch存在,则PrintAncestors函数按从下到上输出二叉树的ch的所有祖先结点序列,格式为一个字符后面跟着一个空格,并返回1,否则返回0。

其中BinTree结构定义如下:

typedef char ElemType;
typedef struct BiTNode {
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
}BiTNode, *BiTree;

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct BiTNode {
    ElementType data;
    struct BiTNode *lchild;
    struct BiTNode *rchild;
    int level;
}BiTNode, *BiTree;
BiTree Create();/*细节在此不表*/
int PrintAncestors(BiTree T,char ch);
int main()
{
    BiTree T = Create();
    char ch;
    scanf("\n%c", &ch);
    int res = PrintAncestors(T,ch);
    if(!res) printf("%c not exist in this tree!",ch);
    return 0;
}
/* 你的代码将被嵌在这里 */

输入样例:

例如对于如下二叉树,输入数据为2行,第一行为由字母和'#'组成的字符串,代表二叉树的扩展先序序列。第二行为指定元素值:

AB#DF##G##C##
F

输出样例: 

对于图中给出的二叉树,输出为:

D B A 

代码示例: 

int PrintAncestors(BiTree T,char ch){
    int jl,jr;
    if(!T) return 0;//空树
    else if(T->data==ch) return 1;//找到ch的祖先,返回1
    else
    {
        jl=PrintAncestors(T->lchild,ch);//递归查找子孙为ch的节点
        jr=PrintAncestors(T->rchild,ch);
        if(jl||jr)
            printf("%c ",T->data);//打印
    }
}

  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值