在释放内存那个函数出了点问题,不知道问题具体出在哪儿,不改了。
#include<CStdio>
#include<CString>
#include<memory>
#include <queue>
#include<iostream>
using namespace std;
struct Node {
bool isvalued;
int v;
Node *left, * right;
Node() :isvalued(false), left(NULL), right(NULL){}
}Tree;
void addNode(Node *T,int vex,char *s) {//(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
int l = strlen(s);
for (int i = 0; i < l; i++) {
if (s[i] == 'L') {
if (!T->left) T->left = new Node;
T = T->left;
}
else if (s[i] == 'R') {
if (!T->right) T->right = new Node;
T = T->right;
}
}
T->v = vex;
T->isvalued = true;
}
bool read_input(Node *T) {
Node *Tree;
Tree = T;
char c[10086]; int i;
scanf("%s", c);
if (!strcmp(c, "()")) return false;
sscanf(&c[1], "%d", &i);
addNode(Tree, i, strchr(c, ',') + 1);
return true;
}
void BFSOut(Node *Tree) {
queue <Node*> Q;
Q.push(Tree);
while (!Q.empty()) {
Node *Temp;
Temp = Q.front();
Q.pop();
if (Temp->isvalued) {
printf("%d ", Temp->v);
if (Temp->left != NULL) Q.push(Temp->left);
if (Temp->right != NULL)Q.push(Temp->right);
}
else printf("-1 ");
}
}
void remove_tree(Node* root) {
if (!root) return;
remove_tree(root->left);
remove_tree(root->right);
delete root;
}
int main() {
while (read_input(&Tree));
BFSOut(&Tree);
remove_tree(&Tree);
return 0;
}