数据结构与算法“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.

求完整代码


#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;
}