拼題A 列出葉結點

拼題A 列出葉結點

題目描述
對於給定的二叉樹,本題要求你按從上到下、從左到右的順序輸出其所有葉節點。
輸入格式:
首先第一行給出一個正整數 N(≤10),爲樹中結點總數。樹中的結點從 0 到 N−1 編號。隨後 N 行,每行給出一個對應結點左右孩子的編號。如果某個孩子不存在,則在對應位置給出 “-”。編號間以 1 個空格分隔。
輸出格式:
在一行中按規定順序輸出葉節點的編號。編號間以 1 個空格分隔,行首尾不得有多餘空格。
輸入樣例:

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

輸出樣例:
4 1 5

沙雕的我直接用鏈表把題目給的樹還原出來然後查找葉結點了。。。。
後來發現,emmm,,,不用這麼麻煩(好像兩種方法也差不多hhh)
兩個版本都AC了~~
上代碼:
版本一:鏈表還原版

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

typedef struct Tree * TreeLink;

struct Tree
{
 int data;
 TreeLink left_tree, right_tree;
};

int n;
int ans[12];
bool is_root[12];
queue<TreeLink> q;

int main()
{
 	memset(is_root, true, sizeof(is_root));
 	
 	cin >> n;
 	
 	char l, r;
 	TreeLink *T;
 	T = (TreeLink *)malloc(sizeof(TreeLink) * 11);
 	
 	for (int i = 0; i < n; i++) // 記得爲每個結點分配內存空間
  		T[i] = (TreeLink)malloc(sizeof(struct Tree));
  		
  	TreeLink root;
 	root = (TreeLink)malloc(sizeof(struct Tree));
 	
 	for (int i = 0; i < n; i++)
 	{
 		getchar();
  		cin >> l >> r;
  		
  		T[i]->data = i;
  		if (l == '-')
   			T[i]->left_tree = NULL;
  		if (r == '-')
  			T[i]->right_tree = NULL;
 		if (l != '-')
  		{
   			int ll = l - '0';
   			is_root[ll] = false;
   			T[i]->left_tree = T[ll];
  		}
  		if (r != '-')
  		{
   			int rr = r - '0';
   			is_root[rr] = false;
   			T[i]->right_tree = T[rr];
  		}
 	}
 	// 找根節點
 	int R;
 	for (int i = 0; i < n; i++)
 	{
  		if (is_root[i])
  		{
   			R = i;
   			break;
  		}
 	}
 	
 	int sum = 0;
 	if (n > 0)
 	{
  		root = T[R];
  		q.push(root);
  		
  		while (!q.empty())
  		{
   			TreeLink temp;
   			temp = q.front();
   			q.pop();
   			
   			if (temp->left_tree == NULL && temp->right_tree == NULL)
    				ans[sum++] = temp->data;
    				
   			if (temp->left_tree != NULL)
    				q.push(temp->left_tree);
    				
   			if (temp->right_tree != NULL)
    				q.push(temp->right_tree);
  		}
 	}
 	
 	for (int i = 0; i < sum; i++)
  		printf("%d%c", ans[i], i == sum - 1 ? '\n' : ' ');
 return 0;
}


版本二:簡練版

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

struct Tree
{
    int data;
    int left_tree, right_tree;
};
int n;
static int flag = 0;

void print(int n)
{
    if(flag)
        printf(" %d", n);
    else
    {
        printf("%d", n);
        flag = 1;
    }
}

Tree T[12];
bool is_root[12];
queue<Tree> q;

int main()
{
    memset(is_root, true, sizeof(is_root));
    scanf("%d", &n);
    char l, r;
    for( int i = 0; i < n; i ++ )
    {
        getchar();
        scanf("%c %c", &l, &r);
        T[i].data = i;
        if(l == '-')
            T[i].left_tree = -1;
        if(r == '-')
            T[i].right_tree = -1;
        if(l != '-')
        {
            int ll = l - '0';
            T[i].left_tree = ll;
            is_root[ll] = false;
        }
        if(r != '-')
        {
            int rr = r - '0';
            T[i].right_tree = rr;
            is_root[rr] = false;
        }
    }
    // 找根節點
    int root;
    for( int i = 0; i < n; i ++ )
    {
        if(is_root[i])
        {
            root = i;
            break;
        }
    }
    
    if( n > 0 )
    {
        q.push(T[root]);
        while(!q.empty())
        {
            Tree t = q.front();
            q.pop();
            
            if(t.left_tree == -1 && t.right_tree == -1)
                print(t.data);
                
            if(t.left_tree != -1)
                q.push(T[t.left_tree]);
                
            if(t.right_tree != -1)
                q.push(T[t.right_tree]);
        }
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章