题意:输入一棵二叉树,你的任务是按从上到下、从左到右的顺序输出各个结点的值。每个结点都按照从根结点都按照从根结点到他的移动序列给出(L表示左,R表示右)。在输入中,每个结点的左括号和右括号之间没有空格,相邻结点之间用一个空格隔开。每棵树的输入用一对空括号"()"结束(这对括号本身不代表一个结点)。注意,如果从跟到某个结点的路径上有的结点没有在输入中给出,或者给出超过一次,应当输出-1。结点个数不超过256.
思路:动态创建二叉树,结构体中多存一个该结点给出值的次数。
动态创建:
#include<bits/stdc++.h>
using namespace std;
char s[300];
struct Node
{
int v, cnt;//值,赋值次数
Node *Left, *Right;
Node():v(0), cnt(0), Left(nullptr), Right(nullptr) {}
};
Node* root;
Node* newnode() { return new Node(); }
void addnode(int v, char* s)
{
Node* u = root;
int len = strlen(s)-1;
for (int i = 0; i < len; i++)
{
if (s[i] == 'L')
{
if (u->Left == nullptr)//左结点不存在就创建一个新结点
u->Left = newnode();
u = u->Left;//往左走
}
else if (s[i] == 'R')
{
if (u->Right == nullptr)//右结点不存在就创建一个新结点
u->Right = newnode();
u = u->Right;//往右走
}
}
u->cnt++;//给值次数
u->v = v;//赋值
}
void remove_tree(Node* u)//释放二叉树
{
if(u == nullptr) return ;
remove_tree(u->Left);
remove_tree(u->Right);
delete u;
}
bool read_input()//输入
{
remove_tree(root);
root = newnode();
while (1)
{
if(scanf("%s", s) != 1) return false;
if (!strcmp(s, "()")) break;
int v;
sscanf(&s[1], "%d", &v);
addnode(v, strchr(s, ',')+1);
}
return true;
}
bool BFS(vector<int>& ans)//层次遍历
{
ans.clear();
queue<Node*> Q;
Q.push(root);
while (!Q.empty())
{
Node* u = Q.front(); Q.pop();
if (u->cnt != 1) return false;
ans.push_back(u->v);
if (u->Left != nullptr) Q.push(u->Left);
if (u->Right != nullptr) Q.push(u->Right);
}
return true;
}
int main()
{
vector<int> ans;
while(read_input())
{
if(!BFS(ans)) { printf("not complete\n"); continue; }
for(int i = 0; i < ans.size(); i++)
{
printf("%d", ans[i]);
if(i != ans.size()-1) printf(" ");
else printf("\n");
}
}
return 0;
}
/*
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
*/
数组模拟:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 300;
char s[MAXN];
int id;
struct Node
{
int v, cnt, Left, Right;
}tree[MAXN];
const int root = 1;//根节点恒为1
void newtree()
{
tree[root].Left = tree[root].Right = tree[root].cnt = 0; id = root;
}
int newnode()
{
int u = ++id; tree[u].Left = tree[u].Right = tree[u].cnt = 0;
return u;
}
int addnode(int v, char* s)
{
int len = strlen(s)-1;
int u = root;
for (int i = 0; i < len; i++)
{
if (s[i] == 'L')
{
if (tree[u].Left == 0)
tree[u].Left = newnode();
u = tree[u].Left;
}
else if (s[i] == 'R')
{
if (tree[u].Right == 0)
tree[u].Right = newnode();
u = tree[u].Right;
}
}
tree[u].cnt++;
tree[u].v = v;
}
bool read_input()//输入
{
newtree();
while (1)
{
if(scanf("%s", s) != 1) return false;
if (!strcmp(s, "()")) break;
int v;
sscanf(&s[1], "%d", &v);
addnode(v, strchr(s, ',')+1);
}
return true;
}
bool BFS(vector<int>& ans)//层次遍历
{
ans.clear();
queue<int> Q;
Q.push(root);
while (!Q.empty())
{
int u = Q.front(); Q.pop();
if (tree[u].cnt != 1) return false;
ans.push_back(tree[u].v);
if (tree[u].Left != 0) Q.push(tree[u].Left);
if (tree[u].Right != 0) Q.push(tree[u].Right);
}
return true;
}
int main()
{
vector<int> ans;
while(read_input())
{
if(!BFS(ans)) { printf("not complete\n"); continue; }
for(int i = 0; i < ans.size(); i++)
{
printf("%d", ans[i]);
if(i != ans.size()-1) printf(" ");
else printf("\n");
}
}
return 0;
}
/*
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
*/
动态创建 + 内存池:
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 300;
char s[MAXN];
struct Node
{
int v, cnt;//值,赋值次数
Node *Left, *Right;
Node():v(0), cnt(0), Left(nullptr), Right(nullptr) {}
}node[MAXN];
Node* root;
queue<Node*> freenodes;//内存池
void init()//内存池初始化
{
for (int i = 0; i < MAXN; i++)
freenodes.push(&node[i]);
}
Node* newnode()//新建结点
{
Node* u = freenodes.front();
u->Left = u->Right = nullptr;
u->cnt = 0;
freenodes.pop();
return u;
}
void addnode(int v, char* s)//增加结点
{
Node* u = root;
int len = strlen(s)-1;
for (int i = 0; i < len; i++)
{
if (s[i] == 'L')
{
if (u->Left == nullptr)
u->Left = newnode();
u = u->Left;
}
else if (s[i] == 'R')
{
if (u->Right == nullptr)
u->Right = newnode();
u = u->Right;
}
}
u->cnt++;
u->v = v;
}
void deletenode(Node* u) { freenodes.push(u); }//删除u结点
void remove_tree(Node* u)//释放该树
{
if(u == nullptr) return;
remove_tree(u->Left);
remove_tree(u->Right);
deletenode(u);
}
bool read_input()
{
remove_tree(root);
root = newnode();
while (1)
{
if(scanf("%s", s) != 1) return false;
if (!strcmp(s, "()")) break;
int v;
sscanf(&s[1], "%d", &v);
addnode(v, strchr(s, ',')+1);
}
return true;
}
bool BFS(vector<int>& ans)
{
ans.clear();
queue<Node*> Q;
Q.push(root);
while (!Q.empty())
{
Node* u = Q.front(); Q.pop();
if (u->cnt != 1) return false;
ans.push_back(u->v);
if (u->Left != nullptr) Q.push(u->Left);
if (u->Right != nullptr) Q.push(u->Right);
}
return true;
}
int main()
{
init();
vector<int> ans;
while(read_input())
{
if(!BFS(ans)) { printf("not complete\n"); continue; }
for(int i = 0; i < ans.size(); i++)
{
printf("%d", ans[i]);
if(i != ans.size()-1) printf(" ");
else printf("\n");
}
}
return 0;
}
/*
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
*/