zoukankan      html  css  js  c++  java
  • 1176:树查找

    题目描述:

    有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY。该树是完全二叉树。

    输入:

    输入有多组数据。
    每组输入一个n(1<=n<=1000),然后将树中的这n个节点依次输入,再输入一个d代表深度。

    输出:

    输出该树中第d层得所有节点,节点间用空格隔开,最后一个节点后没有空格。

    样例输入:
    4
    1 2 3 4
    2
    样例输出:
    2 3
    来源:2010年北京邮电大学网院研究生机试真题
    方法一:
       建二叉树,BFS遍历
    #include <cstdio>
    #include <queue>
    using namespace std;
    struct Node{
        int x,depth;
        Node *lchild;
        Node *rchild;
    }tree[1002];
    int loc;
    Node *create(int temp){
        tree[loc].x = temp;
        tree[loc].lchild = tree[loc].rchild = NULL;
        return &tree[loc++];
    }
    Node* buildTree(int n){
        int temp;
        scanf("%d",&temp);
        Node *root = create(temp);
        root->depth = 1;
        queue<Node*> Q;
        Q.push(root);
        n--;
        while(n){
            Node * pNode = Q.front();
            Q.pop();
            if(n){
               scanf("%d",&temp);
                pNode->lchild = create(temp);
                pNode->lchild->depth = pNode->depth + 1;
                Q.push(pNode->lchild);
                n--;
            }
            if(n){
                scanf("%d",&temp);
                pNode->rchild = create(temp);
                pNode->rchild->depth = pNode->depth + 1;
                Q.push(pNode->rchild);
                n--;
            }
        }
        return root;
    }
    void BFS(Node *root){
        int depth;
        scanf("%d",&depth);
        if(root == NULL)return;
        queue<Node *> Q;
        Q.push(root);
        bool first = true;
        while(!Q.empty()){
            Node *pNode = Q.front();
            if(pNode->depth == depth){
                if(first){
                    printf("%d",pNode->x);
                    first = false;
                }else{
                    printf(" %d",pNode->x);
                }
            }
    
            if(pNode->lchild){
                Q.push(pNode->lchild);
            }
            if(pNode->rchild){
                Q.push(pNode->rchild);
            }
            Q.pop();
        }
        printf("
    ");
    }
    int main(){
        int n;
        while(scanf("%d",&n) != EOF){
            loc = 0;
            Node * root = buildTree(n);
            BFS(root);
        }
    
    
    }

     方法二:

    根据二叉树的特点,如果从1开始标记树节点的序号,左孩子是父亲的序号的两倍,右孩子是父亲的两倍加一。

    //15:42
    #include <cstdio>
    #define N 1002
    int a[N];
    int main(){
        int n,d;
        while(scanf("%d",&n) != EOF){
            for(int i = 1;i <= n;i++)
                scanf("%d",&a[i]);
            scanf("%d",&d);
            //1 2 4
            int start = 1,end;
            d--;
            while(d--){
                start = start * 2;
            }
            if(start > n)printf("EMPTY
    ");
            else{
                if(n < 2 * start)end = n;
                else end = 2*start-1;
                for(int i = start;i < end;i++)
                    printf("%d ",a[i]);
                printf("%d
    ",a[end]);
            }
    
        }
    }
  • 相关阅读:
    设计模式
    Lambda表达式
    网络通信
    排序
    可变参数
    反弹shell学习总结
    Apache Flink任意Jar包上传导致远程代码执行漏洞复现
    定时执行rsync同步数据以及mysql备份
    python练习
    django 模型生成sql(多对多)
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7095497.html
Copyright © 2011-2022 走看看