zoukankan      html  css  js  c++  java
  • 03-树2 List Leaves

    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 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N1. 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  MaxTree 10
    #define  ElementType int
    #define  Tree int 
    #define  Null -1
    
    typedef struct TreeNode
    {
        ElementType Data;
        Tree Left;
        Tree Right;
    }TreeNode;
    
    TreeNode T[MaxTree];
    
    Tree BuildTree(TreeNode T[])
    {
        int N = 0;
        bool Check[MaxTree];
        Tree ret_root = Null;
        char cl, cr;
        cin >> N;
    
        if (N)
        {
            for (int i = 0; i < N;i++)
            {
                Check[i] = false;
            }
            for (int i = 0; i < N;i++)
            {
                T[i].Data = i;
                cin >> cl >> cr;
                if (cl!='-')
                {
                    T[i].Left = cl - '0';
                    Check[T[i].Left] = true;
                }
                else
                {
                    T[i].Left = Null;
                }
                if (cr!='-')
                {
                    T[i].Right = cr - '0';
                    Check[T[i].Right] = true;
                }
                else
                {
                    T[i].Right = Null;
                }
            }
            for (int i = 0; i < N;i++)
            {
                if (!Check[i])
                {
                    ret_root = i;
                    break;
                }
            }
        }
        return ret_root;
    }
    
    void PrintLeaves(Tree  root)
    {
        if (root==Null)
        {
            return;
        }
        queue<int> Q;
        Q.push(root);
        int flag = 1;//输出标志
        Tree temp;
        while (!Q.empty())
        {
            temp = Q.front();
            Q.pop();
            if (T[temp].Left==Null&&T[temp].Right==Null)
            {
                if (flag)
                {
                    cout << T[temp].Data;
                    flag = 0;
                }
                else
                {
                    cout << " " << T[temp].Data;
                }
            
            }
            else
            {
                if (T[temp].Left!=Null)
                {
                    Q.push(T[temp].Left);
                }
                if (T[temp].Right!=Null)
                {
                    Q.push(T[temp].Right);
                }
            }
        }
    }
    
    int main()
    {
        Tree root = Null;
        root = BuildTree(T);
        PrintLeaves(root);
        cout << endl;
        return 0;
    }


  • 相关阅读:
    从输入url到页面加载完成都发生了什么?
    JS 获取和监听屏幕方向变化(portrait / landscape)
    圣杯布局的几种情况
    闭包的使用 — 点击列表项输出项目索引
    论文笔记:dropout
    论文笔记:蒸馏网络(Distilling the Knowledge in Neural Network)
    ng-深度学习-课程笔记-17: 序列模型和注意力机制(Week3)
    ng-深度学习-课程笔记-16: 自然语言处理与词嵌入(Week2)
    ng-深度学习-课程笔记-15: 循环序列模型(Week1)
    Ubuntu安装dlib后import出现libstdc++.so.6: version `GLIBCXX_3.4.21' not found
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/6629589.html
Copyright © 2011-2022 走看看