PAT A1004 Counting Leaves (30 分) 树的遍历

    题目大意:给出一棵树中所有的非叶子节点,以及它们子节点的序号,要求输出每一层的叶子节点的个数。

    和层数有关自然想到的是层序遍历,不过DFS也可以做。输入时记录所有非叶子节点的下标,层序遍历时根据是否是非叶子节点来更新每一层的叶子节点的个数,并记录最大层数。

    如果用DFS,则递归到叶子节点时进行处理。

AC代码如下:

BFS:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstring>

using namespace std;

const int MAXN = 110;
struct Node
{
    int layer;
    vector<int> child;
    Node():layer(0){};
}tree[MAXN];

bool isNotLeaf[MAXN] = {false};
int leafInLayer[MAXN] = {0};

void layerOrder(int node, int &maxLayer)
{
    queue<int> q;
    q.push(node);
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        if(!isNotLeaf[now]) leafInLayer[tree[now].layer]++;
        for (int i = 0; i < tree[now].child.size(); ++i)
        {
            int childId = tree[now].child[i];
            tree[childId].layer = tree[now].layer + 1;
            if(tree[childId].layer > maxLayer) maxLayer = tree[childId].layer;
            q.push(childId);
        }
    }
}

int main()
{
    int N, M;
    cin >> N >> M;
    if(N > 0)
    {
        for (int i = 0; i < M; ++i)
        {
            int id, childNum;
            scanf("%d%d", &id, &childNum);
            for (int j = 0; j < childNum; ++j)
            {
                int childId;
                scanf("%d", &childId);
                tree[id].child.push_back(childId);
            }
            isNotLeaf[id] = true;
        }
        int maxLayer = 0;
        layerOrder(1, maxLayer);
        for (int i = 0; i <= maxLayer; ++i)
        {
            printf("%d", leafInLayer[i]);
            if(i < maxLayer) printf(" ");
        }
    }
    return 0;
}


 

DFS:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstring>

using namespace std;

const int MAXN = 110;
struct Node
{
    int layer;
    vector<int> child;
    Node():layer(0){};
}tree[MAXN];

bool isNotLeaf[MAXN] = {false};
int leafInLayer[MAXN] = {0};

void dfs(int node, int &maxLayer)
{
    if(tree[node].child.size() == 0)
    {
        leafInLayer[tree[node].layer]++;
        if(maxLayer < tree[node].layer) maxLayer = tree[node].layer;
        return;
    }
    for (int i = 0; i < tree[node].child.size(); ++i)
    {
        int childId = tree[node].child[i];
        tree[childId].layer = tree[node].layer + 1;
        dfs(childId, maxLayer);
    }
}

int main()
{
    int N, M;
    cin >> N >> M;
    if(N > 0)
    {
        for (int i = 0; i < M; ++i)
        {
            int id, childNum;
            scanf("%d%d", &id, &childNum);
            for (int j = 0; j < childNum; ++j)
            {
                int childId;
                scanf("%d", &childId);
                tree[id].child.push_back(childId);
            }
            isNotLeaf[id] = true;
        }
        int maxLayer = 0;
        dfs(1, maxLayer);
        for (int i = 0; i <= maxLayer; ++i)
        {
            printf("%d", leafInLayer[i]);
            if(i < maxLayer) printf(" ");
        }
    }
    return 0;
}


 

你可以这样解析出 map 中的 fullDocument 中的 OwnerID、name、QuotaSize: ``` package main import ( "fmt" "reflect" ) func main() { m := map[string]interface{}{ "_id": map[string]interface{}{ "_data": "8263B4EB63000000062B022C0100296E5A1004C3E4524B77B64630AC204C7469FAED7F46645F6964006463B4EB636EBBDB249F2ED2770004", }, "clusterTime": map[string]interface{}{ "1672801123": 6, }, "documentKey": map[string]interface{}{ "_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")", }, "fullDocument": map[string]interface{}{ "OwnerID": 123, "QuotaSize": 104857600, "_id": "ObjectID(\"63b4eb636ebbdb249f2ed277\")", "directory": "/buckets", "name": "123_test1", }, "ns": map[string]interface{}{ "coll": "UserQuotaConfig", "db": "filer3", }, "operationType": "insert", "wallTime": 1672801123879, } // 取出 fullDocument fullDocument, ok := m["fullDocument"].(map[string]interface{}) if !ok { fmt.Println("fullDocument not found") return } // 取出 OwnerID OwnerID, ok := fullDocument["OwnerID"].(int) if !ok { fmt.Println("OwnerID not found") return } fmt.Println("OwnerID:", OwnerID) // 取出 name name, ok := fullDocument["name"].(string) if !ok { fmt.Println("name not found") return } fmt.Println("name:", name) // 取出 QuotaSize QuotaSize, ok := fullDocument["QuotaSize"].(int) if !ok { fmt.Println("QuotaSize not found") return } fmt.Println("QuotaSize:", QuotaSize) } ``` 在这个例子中,我们首先将 map 赋值给了变量 m。然后使用类型断言取出了 fullDocument 并将其赋
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值