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);
}
}
判断谁在左边挺巧妙