关闭

03-树2 List Leaves

标签: 数据结构
203人阅读 评论(0) 收藏 举报
分类:

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 NN (\le 10≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N-1N−1. Then NN 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 <iostream>
#include <queue>
using namespace std;

//#define Elementtype int
#define null -1
typedef struct Bintree
{
    //Elementtype data;
    int left;
    int right;
}Bintree;
Bintree* bintree;
int treenum ;
int* flag;
void input()
{

    cin >> treenum;

    bintree = new Bintree[treenum];
    flag = new int[treenum];

    char temp = 0;
    for (int i = 0; i < treenum; i++)
    {
        cin >> temp;
        if (temp == '-')
        {
            bintree[i].left = -1;
        }
        else
        {
            bintree[i].left = temp - '0';
            flag[bintree[i].left] = 1;
        }

        cin >> temp;
        if (temp == '-')
        {
            bintree[i].right = -1;
        }
        else
        {
            bintree[i].right = temp - '0';
            flag[bintree[i].right] = 1;
        }

    }
    delete[]bintree;
    delete[]flag;
}

int findtop()
{
    for (int i = 0; i < treenum; i++)
    {

        if (flag[i] != 1)
            return i;
    }
}

void traverse()
{
    int top = findtop();

    queue<int> q;
    q.push(top);

    while (!q.empty())
    {
        top = q.front();
        q.pop();

        if (bintree[top].left != null)
        {
            q.push(bintree[top].left);
        }
        if (bintree[top].right != null)
        {
            q.push(bintree[top].right);
        }
        if (bintree[top].left == null&&bintree[top].right == null)
        {
            if (!q.empty())
                cout << top << ' ';
            else
                cout << top;
        }
    }


}

int main()
{
    input();
    traverse();


    return 0;
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

(2016秋数据结构课后练习题总结)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. ...
  • jiaomenglei
  • jiaomenglei
  • 2016-09-16 21:26
  • 1110

【解题报告】【浙大PAT】03-树1. List Leaves (25)

03-树1. List Leaves (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B ...
  • linhui0705
  • linhui0705
  • 2015-03-31 20:12
  • 204

pat 03-树2 List Leaves(mooc 陈越、何钦铭-数据结构)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. ...
  • tdscdma123
  • tdscdma123
  • 2016-09-19 20:48
  • 349

数据结构——List Leaves

题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to ri...
  • cxq569503960
  • cxq569503960
  • 2016-10-26 17:23
  • 46

ios开发——经典翻页库Leaves源码解析

在做文本app的时候经常会用到翻页效果,我了解的翻页效果有三种。最基本的是用transition动画的CurlDown和CurlUp来实现,这种实现非常简单,但是不能控制过程。然后是类似开源库Leav...
  • u013604612
  • u013604612
  • 2014-10-11 21:06
  • 930

树2. List Leaves(树的遍历,队列的简单应用)

首先参考ice_camel的博客,其实现方式非常简洁便于理解 题目要求 Given a tree, you are supposed to list all the leaves ...
  • as14569852
  • as14569852
  • 2017-06-13 10:47
  • 106

List Leaves【数据结构测试3.2】

题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to rig...
  • songyu0120
  • songyu0120
  • 2015-01-25 19:38
  • 538

Java中List转List树的通用解决方法~

首先是父接口,想要用这个通用List树生成的话就必须实现这个接口 import java.util.List; /** * @author Mu * */ public interface T...
  • j506825719
  • j506825719
  • 2016-08-02 09:10
  • 3037

03-树1 树的同构

#include #define MaxTree 10 typedef struct TreeNode{ char element; int left; int right; } BinT...
  • asunamashiro
  • asunamashiro
  • 2015-09-22 17:16
  • 455

03-树1 树的同构

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。...
  • SaltedfishSun
  • SaltedfishSun
  • 2017-03-21 19:41
  • 126
    个人资料
    • 访问:14758次
    • 积分:412
    • 等级:
    • 排名:千里之外
    • 原创:26篇
    • 转载:0篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论