欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

List Leaves

程序员文章站 2022-05-17 21:17:18
...
题目链接:http://www.patest.cn/contests/mooc-ds/03-2

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

 解题思路:

1、用二维数组储存数据

2、找到根节点

3、把各个节点放在数组中组成树

4、递归遍历树并给各个节点做标记

5、按照标记顺序输出

注意事项

1、对TreeNode的操作不复杂,不用自己写析构函数、赋值函数

2、输出格式需要注意

#include <iostream>
#include <string>
#include<sstream>
using namespace std;

typedef struct  TreeNode  *Bintree;

struct  TreeNode{
	TreeNode(){
		Data = 0;
		Left = nullptr;
		Right = nullptr;
	}
	int Data;
	Bintree Left;
	Bintree Right;
};

TreeNode c[11];
int d[11] = { 0 };

void PreOrderTraversal(Bintree BT)
{
	if (BT)
	{
		if (BT->Left != nullptr)
		{
			d[(BT->Left)->Data] = d[BT->Data] * 2;
		}
		if (BT->Right != nullptr)
		{
			d[(BT->Right)->Data] = d[BT->Data] * 2+1;
		}
		PreOrderTraversal(BT->Left);
		PreOrderTraversal(BT->Right);
	}
}

int main(){
	int number;
	cin >> number;
	int a[11][2];
	int b[11] = { 0 };
	for (int i = 0; i < number; i++)
	{
		string s1, s2;
		int i1, i2;
		cin >> s1 >> s2;
		if (s1 == "-")
		{
			i1 = 100;
		}
		else{
			stringstream ss;
			ss << s1;
			ss >> i1;
		}
		if (s2 == "-")
		{
			i2 = 100;
		}
		else{
			stringstream ss;
			ss << s2;
			ss >> i2;
		}
		a[i][0] = i1;
		a[i][1] = i2;
		b[i1] = 1;
		b[i2] = 1;
	}
	int rootNum;
	for (int i = 0; i < number; i++)
	{
		if (b[i] == 0)
		{
			rootNum = i;
			break;
		}
	}
	
	for (int i = 0; i < number; i++)
	{
		c[i].Data = i;
		if (a[i][0] != 100)
			c[i].Left = &c[a[i][0]];
		if (a[i][1] != 100)
			c[i].Right = &c[a[i][1]];
	}
	d[rootNum] = 1;
	PreOrderTraversal(&c[rootNum]);
	for (int i = 0; i < number; i++)
	{
		if (a[i][0]!=100  || a[i][1]!=100)
		{
			d[i] = 10000;
		}
	}
	int p;
	int flag = 1;
	for (int i = 0; i < number;i++)
	{
		int min = 10000;
		for (int j = 0; j < number;j++)
		{
			if (d[j]<min)
			{
				p = j;
				min = d[j];
			}
		}
		if (min == 10000)
		{
			break;
		}
		else
		{
		if (flag == 1)
			flag = 0;
		else
			cout << " ";
		}
		cout << p ;
		d[p] = 10000;
	}
	return 0;
}

优秀题解:
转自:http://blog.csdn.net/u012762625/article/details/43570799

#include <bits/stdc++.h>
using namespace std;
struct Node {
    int lchild;
    int rchild;
    Node() {
        lchild = -1;
        rchild = -1;
    }
};
Node node[20];
int n, flag = 0;
bool findroot[20];
char l, r;
queue<int> q;
void bfs(int i) {
    q.push(i);
    while(!q.empty()) {
        int cur = q.front();
        q.pop();
        if(node[cur].lchild == -1 && node[cur].rchild == -1) {
            if(flag == 0) {
                printf("%d", cur);
                flag = 1;
            } else {
                printf(" %d", cur);
            }
        }
        if(node[cur].lchild != -1) {
            q.push(node[cur].lchild);
        }
        if(node[cur].rchild != -1) {
            q.push(node[cur].rchild);
        }
    }
    printf("\n");
}
int main() {
    memset(findroot, false, sizeof(findroot));
    scanf("%d", &n);
    getchar();
    for (int i = 0; i < n; i ++) {
        scanf("%c %c", &l, &r);
        getchar();
        if(isdigit(l)) {
            node[i].lchild = l - '0';
            findroot[node[i].lchild] = 1;
        }
        if(isdigit(r)) {
            node[i].rchild = r - '0';
            findroot[node[i].rchild] = 1;
        }
    }
    for (int i = 0; i < n; i ++) {
        if(findroot[i] == 0) {
            bfs(i);
            break;
        }
    }
    return 0;
}

  吸取经验:

1、灵活使用队列

2、用简洁的方式达到输出要求

3、思维不要僵化,不一定要构建树