2021-04-23

03-树2 List Leaves (25 分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5
#include<stdlib.h>
struct BinTree {
	int Name;
	struct BinTree* Left;
	struct BinTree* Right;
	int Level;
	int Lvalue;

};
typedef struct BinTree* PtrToBinTree;
PtrToBinTree* CreateArrary(int N);
void Get_Level(PtrToBinTree a);
PtrToBinTree InitlizeArrary(PtrToBinTree* Arrary, int N, int* Leaf, int* LeafNumber);
void MidTraverse(PtrToBinTree a);
void ChooseSortbyLevel(PtrToBinTree* Arrary, int* Leaf, int LeafNumber);
void PrintNode(PtrToBinTree* Arrary, int* Leaf, int LeafNumber);
int CommonLvalue=1;
int main()
{
	int N, LeafNumber;
	scanf("%d", &N);
	int* Leaf = (int*)malloc(sizeof(int) * N);
	PtrToBinTree* Arrary = CreateArrary(N);
	PtrToBinTree Root = InitlizeArrary(Arrary, N, Leaf, &LeafNumber);
	Root->Level = 1;
	Get_Level(Root);
	MidTraverse(Root);//成功
	ChooseSortbyLevel(Arrary, Leaf, LeafNumber);
	PrintNode(Arrary, Leaf, LeafNumber);

	
}
PtrToBinTree* CreateArrary(int N)
{
	PtrToBinTree* Arrary = (PtrToBinTree*)malloc(sizeof(PtrToBinTree) * N);//存放指针的数组
	for (int i = 0; i < N; i++)
	{
		Arrary[i] = (PtrToBinTree)malloc(sizeof(struct BinTree));
		Arrary[i]->Name = i;
		Arrary[i]->Left = NULL;//表示其无左右孩子
		Arrary[i]->Right = NULL;
	}
	return Arrary;
}
PtrToBinTree InitlizeArrary(PtrToBinTree* Arrary, int N,int*Leaf,int*LeafNumber)
{
	char L, R;
	int Flag;
	int j=0;
	int RootNumber;
	int* a = (int*)malloc(sizeof(int) * N);
	for (int i = 0; i < N; i++)
	{
		a[i] = 0;
	}
	for (int i = 0; i < N; i++)
	{
		Flag = 1;
		scanf(" %c %c", &L, &R);
		if (L != '-')
		{
			Arrary[i]->Left = Arrary[L - '0'];//左孩子
			a[L - '0'] = 1;
			Flag = 0;
		}
		if (R != '-')
		{
			Arrary[i]->Right = Arrary[R - '0'];//右孩子
			a[R - '0'] = 1;//不能是根
			Flag = 0;
		}
		if (Flag == 1)
		{
			Leaf[j++] = i;
		}
	}
	for (int i = 0; i < N; i++)
	{
		if (a[i] == 0)
		{
			RootNumber = i;//找根
			break;
		}
	}
	*LeafNumber = j;//叶节点数
	return Arrary[RootNumber];
}
void Get_Level(PtrToBinTree a)
{
	if (a->Left)
	{
		a->Left->Level = a->Level + 1;
		Get_Level(a->Left);
	}
	if (a->Right)
	{
		a->Right->Level = a->Level + 1;
		Get_Level(a->Right);
	}

}
void MidTraverse(PtrToBinTree a)
{
	if (a)
	{
		MidTraverse(a->Left);
		a->Lvalue = CommonLvalue;
		CommonLvalue++;
		MidTraverse(a->Right);
		
	}
}
void ChooseSortbyLevel(PtrToBinTree* Arrary, int* Leaf, int LeafNumber)
{
	int Tmp;
	int Min;
	for (int i = 0; i < LeafNumber - 1; i++)
	{
		Tmp = Leaf[i];
		Min = i;
		for (int j = i + 1; j < LeafNumber; j++)
		{
			if (Arrary[Leaf[Min]]->Level > Arrary[Leaf[j]]->Level)
			{
				Min = j;
			}

		}
		Leaf[i] = Leaf[Min];
		Leaf[Min] = Tmp;
	}


}
void PrintNode(PtrToBinTree* Arrary, int* Leaf, int LeafNumber)
{
	int MinLevel, MinLeftValue,PrintNode,index;
	for (int i = 0; i < LeafNumber; i++)
	{
		int Flag = 1;//初次进入循环标志

		for (int j = 0; j < LeafNumber; j++)
		{
			if (Leaf[j] == -1)
			{
				continue;
			}
			else
			{
				if (Flag == 1)
				{
					PrintNode = Leaf[j];
					MinLeftValue = Arrary[Leaf[j]]->Lvalue;
					MinLevel = Arrary[Leaf[j]]->Level;
					Leaf[j] = -1;
					index = j;//记录在数组中的标识
					Flag = 0;
				}
				else
				{
					if (Arrary[Leaf[j]]->Level > MinLevel)
						break;
					else 
					{
						if (Arrary[Leaf[j]]->Lvalue < MinLeftValue)
						{
							Leaf[index] = PrintNode;
							PrintNode = Leaf[j];
							index = j;
							MinLeftValue = Arrary[Leaf[j]]->Lvalue;
							Leaf[j] = -1;

						}
					}
				}
			}
		}
		if(i<LeafNumber-1)
		printf("%d ", PrintNode);
		else
		printf("%d", PrintNode);

	}
}

判断谁在左边挺巧妙

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值