• List Leaves


    • 题目描述

    • 题目思路

    1 树的建立可以使用静态链表法。
    2 题目要求从上到下,从左到右的顺序,就是对树进行层序遍历,层序遍历需要用到队列这种数据结构。
    3 题目的输出要求“行尾不能有多余的空格”,可以把要输出的节点放到一个数组里,然后循环输出节点和空格,到最后一个节点时,只输出节点即可。

    • C代码实现
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdbool.h>
    
    #define TMaxSize 10
    
    typedef int ElementType;
    
    int Leafnode[TMaxSize];
    
    struct QNode
    {
    	int* Data;  //存储元素的数组
    	int Front;  //队列的头指针
    	int Rear;  //队列的尾指针
    	int MaxSize;  //队列的最大容量
    };
    
    struct TNode
    {
    	ElementType elem;
    	int Left;
    	int Right;
    }T1[TMaxSize];
    
    
    //创建一个队列
    struct QNode* CreateQueue(int MaxSize)
    {
    	struct QNode* Q = (struct QNode*)malloc(sizeof(struct QNode));
    	Q->Data = (int *)malloc(MaxSize * sizeof(int));
    	Q->Front = Q->Rear = 0;
    	Q->MaxSize = MaxSize;
    	return Q;
    }
    
    bool IsFull(struct QNode* Q)
    {
    	return ((Q->Rear + 1) % Q->MaxSize) == Q->Front;
    }
    
    //在队列尾插入一个元素
    //参数 struct QNode* Q 要操作的队列
    //     int x  待插入的元素
    bool AddQ(struct QNode* Q, int x)
    {
    	if (IsFull(Q))  //判断队列是否为空
    	{
    		printf("队列满,不能再插入元素
    ");
    		return false;
    	}
    	else
    	{
    		Q->Rear = (Q->Rear + 1) % Q->MaxSize;
    		Q->Data[Q->Rear] = x;
    		return true;
    	}
    }
    
    //判断队列是否为空
    bool IsEmpty(struct QNode* Q)
    {
    	return (Q->Front == Q->Rear);
    }
    //在队列头部删除一个元素
    int DeleteQ(struct QNode* Q)
    {
    	if (IsEmpty(Q))
    	{
    		printf("队列为空
    ");
    		return false;
    	}
    	else
    	{
    		Q->Front = (Q->Front + 1) % Q->MaxSize;
    		return Q->Data[Q->Front];
    	}
    
    }
    
    int BuildTree(struct TNode T[])
    {
    	int N;
    	int check[TMaxSize];
    	int i = 0;
    	char cl, cr;
    	int Root;
    	scanf("%d",&N);
    	getchar();
    	if (N != 0)
    	{
    		for (i = 0;i < N;i++)
    		{
    			check[i] = 0;
    		}
    		for (i = 0;i < N;i++)
    		{
    			scanf("%c %c",&cl,&cr);
    			getchar();
    			T[i].elem = i;
    			if (cl != '-')
    			{
    				T[i].Left = cl - '0';
    				check[T[i].Left] = 1;
    			} 
    			else
    			{
    				T[i].Left = -1;
    			}
    			if (cr != '-')
    			{
    				T[i].Right = cr - '0';
    				check[T[i].Right] = 1;
    			}
    			else
    			{
    				T[i].Right= -1;
    			}
    		}
    
    		for (i = 0;i < N;i++)
    		{
    			if (!check[i])
    			{
    				Root = i;
    				break;
    			}
    		}
    	}
    	return Root;
    }
    //使用队列实现层序遍历
    void Traversal(int Root)
    { 
    	int T;
    	int i = 0;
    	int j = 0;
    	struct QNode* Q = CreateQueue(100);
    	AddQ(Q,Root);
    	while (!IsEmpty(Q))
    	{
    		T = DeleteQ(Q);
    		if (T1[T].Left == -1 && T1[T].Right == -1)
    		{
    			Leafnode[i] = T;
    			i++;
    		}
    		if (T1[T].Left != -1)
    		{
    			AddQ(Q, T1[T1[T].Left].elem);
    		}
    		if (T1[T].Right != -1)
    		{
    			AddQ(Q, T1[T1[T].Right].elem);
    		}
    	}
    	while (j < i - 1)
    	{
    		printf("%d ",Leafnode[j]);
    		j++;
    	}
    	printf("%d", Leafnode[i-1]);
    }
    
    int main()
    {
    	int R1;
    	R1 = BuildTree(T1);
    	Traversal(R1);
    	// system("pause");
    	return 0;
    }
    
  • 相关阅读:
    本地复现Zabbix v2.2.x, 3.0.0-3.0.3 jsrpc 参数 profileIdx2 SQL 注入漏洞
    本地搭建复现st2-045漏洞
    Ubuntu安装Vulapps漏洞靶场
    如何在腾讯云Ubuntu服务器安装kali下的神器
    nginx 跳转配置
    Chocolatey 的安装
    MySQL 5.1 主从同步配置
    针对Windows Server 2008 Web 服务 IIS+php 配置的一些心得
    解决IIS7+php的组合上传限制30M的问题
    ssh 文件权限影响登录
  • 原文地址:https://www.cnblogs.com/Manual-Linux/p/11428481.html
Copyright © 2020-2023  润新知