首页 > ACM题库 > 九度OJ > 九度-1521-二叉树的镜像[解题代码]
2013
12-13

九度-1521-二叉树的镜像[解题代码]

题目描述:

输入一个二叉树,输出其镜像。

输入:

输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000,n代表将要输入的二叉树节点的个数(节点从1开始编号)。接下来一行有n个数字,代表第i个二叉树节点的元素的值。接下来有n行,每行有一个字母Ci。
Ci=’d’表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号。
Ci=’l’表示第i个节点有一个左孩子,紧接着是左孩子的编号。
Ci=’r’表示第i个节点有一个右孩子,紧接着是右孩子的编号。
Ci=’z’表示第i个节点没有子孩子。

输出:

对应每个测试案例,
按照前序输出其孩子节点的元素值。
若为空输出NULL。

样例输入:
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

cpp 代码如下:
#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;
}
/**************************************************************
	Problem: 1521
	User: coder
	Language: C
	Result: Accepted
	Time:0 ms
	Memory:936 kb
****************************************************************/


  1. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  2. #include <stdio.h>
    int main(void)
    {
    int arr[] = {10,20,30,40,50,60};
    int *p=arr;
    printf("%d,%d,",*p++,*++p);
    printf("%d,%d,%d",*p,*p++,*++p);
    return 0;
    }

    为什么是 20,20,50,40,50. 我觉得的应该是 20,20,40,40,50 . 谁能解释下?

  3. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.