저장 성 빅 데이터 구조 - 출력 트 리 의 잎 사 귀 노드

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 
#include 
using namespace std;
#define Null -1
#define Tree int
#define N 10


class Treenode{
public:
    int  left;
    int right;
};
Treenode T[N];

Tree Buildtree(Treenode * T){
    int i;
    int n;
    char r,l;
    Tree root;
    cin>>n;
    int check[n];
    for(i=0;i> l >> r;
            if (l != '-') {
                T[i].left = l - '0';
                check[T[i].left] = 1;
            } else T[i].left = Null;
            if (r!='-'){
                T[i].right=r-'0';
                check[T[i].right]=1;
            }
            else T[i].right=Null;
        }
        for(i=0;i q;
    if(root==Null) return ;
    q.push(root);
    while(!q.empty()){
        Tree head=q.front();
        if((T[head].right==Null)&&(T[head].left==Null)) {
            if (q.size() == 1)cout << head << endl;
            else cout << head << " ";
        }
        q.pop();
        if(T[head].left!=Null){
            q.push(T[head].left);
        }
        if(T[head].right!=Null){
            q.push(T[head].right);
        }

    }
}



int main (){
    Tree t;
    t=Buildtree(T);
    find_leaves(t);
    return 0;
}

좋은 웹페이지 즐겨찾기