P3391【模板】文藝平衡樹
阿新 • • 發佈:2020-12-07
#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; }