uicccui 2021-11-24 18:20 采纳率: 42.9%
浏览 30
已结题

数据结构与算法“play with tree”

Given a binary tree, your task is to get the height and size(number of nodes) of the tree.
The Node is defined as follows.

struct Node {
Node *lc, *rc;
char data;
};

Implement the following function.

void query(const Node *root, int &size, int &height)
{
// put your code here
}

提示

In this problem, we assume that the height of the tree with only one node is 1.

求完整代码

  • 写回答

1条回答 默认 最新

  • panfei625 2021-11-24 18:24
    关注
    
    #include <bits/stdc++.h>
    using namespace std;
    struct node
    {
        char data;
        struct node *lc, *rc;
    };
    char a[100];
    int num = 0;
    struct node *creat()
    {
        struct node *root;
        if(a[num++] == ',')
        {
            root = NULL;
        }
        else {
            root = new node;
            root -> data = a[num - 1];
            root -> lc = creat();
            root -> rc = creat();
        }
        return root;
    };
    void fin(struct node *root)
    {
        struct node *q[100];
        int in = 0, out = 0;
        q[in ++] = root;
        while(out < in){
        if(q[out])
        {
           if(q[out] -> lc == NULL && q[out] -> rc == NULL) printf("%c",q[out]->data);
            q[in++] = q[out]->lc;
            q[in++] = q[out] -> rc;
        }
        out ++;
        }
    }
    int main()
    {
        while(~scanf("%s", a))
        {
            struct node *root;
            num =0;
            root = creat();
            fin(root);
            printf("
    ");
        }
        return 0;
    }
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月24日
  • 创建了问题 11月24日

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站