二叉树

#include <bits/stdc++.h>
using namespace  std;
#define ll long long
struct edge{
   int data;
   struct edge *left;
   struct edge *right;
};
struct edge *build(struct edge *T, char F)
{
    struct edge *p;
    p = (edge*)malloc(sizeof(edge));
    if(!T)
    {
        puts("是否输入根节点");
        char a = getchar();
        while (a!='N'&&a!='Y')getchar();
        if (a == 'N')return NULL;
        int x;
        puts("请输入x");
        cin>>x;
        p -> data = x;
    }
    else
    {
        if (F == 'L')
        {
            puts("是否输入左根节点");
            char a = getchar();
            while (a!='N'&&a!='Y')getchar();
            if (a == 'N')return NULL;
            int x;
            puts("请输入左节点x");
            cin>>x;
            p -> data =x;
        }
        else if (F == 'R')
        {
            puts("是否输入右根节点");
            char a = getchar();
            while (a!='N'&&a!='Y')getchar();
            if (a == 'N')return NULL;
            int x;
            puts("请输入右节点x");
            cin>>x;
            p -> data = x;
        }
    }
    p->left = build(p,'L');
    p->right = build(p,'R');
    return p;
}
void put(struct edge *T)
{
    if ( T == nullptr )return;
    cout<<T->data<<endl;
    put(T->left);
    put(T->right);
}
int main()
{
    struct edge *T;
    T = build(NULL,' ');
    put(T);
    return 0;
}
齐芒行,川锋明!
原文地址:https://www.cnblogs.com/qimang-311/p/13777711.html