| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 买学问 - 大牛一对一辅导，有问必答 买学问 - 专业的付费知识问答平台

已结贴   问题点数：20  回复次数：8

#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int maxn = 100;
bool Isnodroot[maxn] = {false};
struct Node{
//    int data;c
int lchild;
int rchild;
}node[maxn];
int n, num = 0;
void print(int index) {

printf("%d",index);
num++;
if(num < n) printf(" ");
else printf("\n");

}
void inorder(int root) {
if(root == -1)   return;
inorder(node[root].lchild);
print(root);
inorder(node[root].rchild);
}

void bfs(int root){
queue<int> q;
q.push(root);
while( !q.empty() ){
int front = q.front();

q.pop();
print(front);
if(node[front].lchild != -1)  {
q.push(node[front].lchild);
}
if(node[front].rchild != -1)
{
q.push(node[front].rchild);
}
}

}
void postInvert(int root){
if(root == -1)  return;
postInvert(node[root].lchild);
postInvert(node[root].rchild);
swap(node[root].lchild, node[root].rchild);
}
int nodroot(){
for(int i = 0; i < n; i++) {
if(Isnodroot[i] == false) return i;
}
}
int str2Num(char c) {
if(c == '-') return -1;
else{
Isnodroot[c - '0'] = true;
return c - '0';
}
}

int main()
{
freopen("1.txt","r",stdin);
string a;
scanf("%d", &n);
for(int i = 0; i < n; i++) {

scanf("%s",&a);
getchar();
if(a[0] != '-') {
node[i].lchild = a[0] - '0';
Isnodroot[node[i].lchild] = true;
}else node[i].lchild = -1;
if(a[1] != '-') {
node[i].rchild = a[1] - '0';
Isnodroot[node[i].rchild] = true;
}else node[i].rchild = -1;
}
int root = nodroot();
postInvert(root);
bfs(root);
num = 0;
inorder(root);
return 0;
}

1.txt 中的内容:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
得分:10
string a;
……
scanf("%s",&a);

得分:10

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

得分:0
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
得分:0

得分:0

得分:0

sorry,没系统学过c++，出现如此低级的错误。

得分:0

cin>> a;
getchar();
if(a[0] != '-') {
node[i].lchild = a[0] - '0';
Isnodroot[node[i].lchild] = true;
}else node[i].lchild = -1;
if(a[1] != '-') {
node[i].rchild = a[1] - '0';
Isnodroot[node[i].rchild] = true;
}else node[i].rchild = -1;

```    for(int i = 0; i < n; i++)
{
char a[2];
cin >> a[0] >> a[1];

if(a[0] != '-') {
node[i].lchild = a[0] - '0';
Isnodroot[node[i].lchild] = true;
}else node[i].lchild = -1;
if(a[1] != '-') {
node[i].rchild = a[1] - '0';
Isnodroot[node[i].rchild] = true;
}else node[i].rchild = -1;
}```

```#include <cstdio>
#include <queue>

void level_order( const char a[][2], unsigned n, unsigned root )
{
std::queue<unsigned> qe;
qe.push( root );

while( !qe.empty() )
{
unsigned f = qe.front();
qe.pop();

if( a[f][1] != '-' )
qe.push( a[f][1]-'0' );

if( a[f][0] != '-' )
qe.push( a[f][0]-'0' );

printf( "%u", f );
if( !qe.empty() )
putchar( ' ' );
}
}

void in_order( const char a[][2], unsigned n, unsigned root )
{
if( a[root][1] != '-' )
{
in_order( a, n, a[root][1]-'0' );
putchar( ' ' );
}

printf( "%u", root );

if( a[root][0] != '-' )
{
putchar( ' ' );
in_order( a, n, a[root][0]-'0' );
}
}

int main( void )
{
// 输入
unsigned n;
char a[10][2];
{
scanf( "%u", &n );
for( unsigned i=0; i!=n; ++i )
scanf( " %c %c", &a[i][0], &a[i][1] );
}

// 获得root
unsigned root;
{
root = n*(n-1)/2;
for( unsigned i=0; i!=n; ++i )
root -= (a[i][0]=='-'?0:a[i][0]-'0') + (a[i][1]=='-'?0:a[i][1]-'0');
}

// 层序遍历
level_order( a, n, root );
putchar( '\n' );

// 中序遍历
in_order( a, n, root );
putchar( '\n' );
}```

得分:0

• 9
• 1/1页
• 1