2013
12-28

# 剑指offer(11)-二叉树的镜像[数据结构]

Ci=’d’表示第i个节点有两子孩子，紧接着是左孩子编号和右孩子编号。
Ci=’l’表示第i个节点有一个左孩子，紧接着是左孩子的编号。
Ci=’r’表示第i个节点有一个右孩子，紧接着是右孩子的编号。
Ci=’z’表示第i个节点没有子孩子。

7
8 6 10 5 7 9 11
d 2 3
d 4 5
d 6 7
z
z
z
z

8 10 11 9 6 7 5

//============================================================================
// Name        : reverseTree.cpp
// Author      : coder
// Version     :
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
int n, a, i;
typedef struct Node {
struct Node * l;
struct Node * r;
int data;
}* pNode, Node;
char temp[2];
Node tree[1001];
//先右后左输出二叉树
void print(pNode root) {
if (root == 0)
return;
printf(" %d", root->data);
print(root->r);
print(root->l);
}
int main() {
while (~scanf("%d", &n)) {
for ( i = 1; i <= n; i++) {
scanf("%d", &tree[i].data);
tree[i].l = tree[i].r = 0;
}
for ( i = 1; i <= n; i++) {
scanf("%s %d", temp, &a);
if (temp[0] == 'd') {
tree[i].l = &tree[a];
scanf("%d", &a);
tree[i].r = &tree[a];
}else if(temp[0] == 'l')
tree[i].l = &tree[a];
else if(temp[0] == 'r')
tree[i].r = &tree[a];
}
if (n) {
printf("%d", tree[1].data);//由于空格判断，根节点单独输出
print(tree[1].r);
print(tree[1].l);
puts("");
} else
puts("NULL");
}
return 0;
}

1. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish