PAT 1020 Tree Traversals

1020. Tree Traversals (25)

时间限制     400 ms
内存限制     32000 kB
代码长度限制   16000 B
判题程序     Standard

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2


让我纠结了一个晚上,后来树是建立起来了。但是打印的方法想了半天,最后网上参考了一位仁兄的思路。
http://blog.csdn.net/leisore/article/details/6117335
不过我做了一点改进,直接使用一个队列。循环push就可以了

原思路如下:

 使用两个辅助队列层序打印二叉树。思路是:

    1. root进入队列A
    2. 队列A的元素依次出对,并做两件事:
  1. 打印自己
  2. 让自己的左右孩子依次进入队列B
  1. 队列B的元素依次出对队,并做两件事:
  1. 打印自己
  2. 让自己的左右孩子依次进入队列A

     重复步骤2,3直到队列A、B均为空

  我的思路是:

  1. root进入队列A
  2. 队列A的第一个元素出队,并做两件事:
  1. 打印自己
  2. 让自己的左右孩子依次进入队列A的末尾

  重复直到队列A为空



代码:
//============================================================================
// Name        : Test.cpp
// Author      : wenlong
// Version     :
// Copyright   : All rights reserved!
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

struct node {
	node* left, *right;
	int value;
};

int n;

vector<int> in, post;

node* getParent(int start, int end) {
	node* pa = new node();

	pa->right = NULL;
	pa->left = NULL;

	pa->value = post[post.size() - 1];

	int i, paIndex = -1;
	for (i = start; i <= end; i++) {
		if (in[i] == pa->value) {
			paIndex = i;
			break;
		}
	}
	if (paIndex == -1) {
		//Sample Input:
		//7
		//2 3 1 5 7 6 4
		//1 2 3 4 5 6 7
		//Sample Output:
		//4 1 6 3 5 7 2
	} else if (paIndex < end) {
		post.pop_back();
		pa->right = getParent(paIndex + 1, end);
		if (paIndex == start) {
			pa->left = NULL;
		} else if (paIndex > start) {
			post.pop_back();
			pa->left = getParent(start, paIndex - 1);
		}
	} else if (paIndex == end) {
		pa->right = NULL;
		if (paIndex == start) {
			pa->left = NULL;
		} else if (paIndex > start) {
			post.pop_back();
			pa->left = getParent(start, end - 1);
		}
	}

	return pa;
}

void printInPostOrder(node* p) {
	if (p->left != NULL) {
		printInPostOrder(p->left);
	}
	if (p->right != NULL) {
		printInPostOrder(p->right);
	}

	cout << p->value << " ";
}

queue<node*> a;
vector<int> out;

void printInLevelOrder(node* p) {
	a.push(p);

	while (a.size() > 0) {
		node* t = a.front();
		a.pop();
		out.push_back((t->value));
		if (t->left != NULL) {
			a.push(t->left);
		}
		if (t->right != NULL) {
			a.push(t->right);
		}
	}

}

void printInInOrder(node* p) {
	if (p->left != NULL) {
		printInInOrder(p->left);
	}

	cout << p->value << " ";

	if (p->right != NULL) {
		printInInOrder(p->right);
	}

}

int main() {

	cin >> n;

	node *parent;

	int i, temp;

	//后序
	for (i = 0; i < n; ++i) {
		cin >> temp;
		post.push_back(temp);
	}

	//中序
	for (i = 0; i < n; ++i) {
		cin >> temp;
		in.push_back(temp);
	}

	parent = getParent(0, n - 1);

	//printInPostOrder(parent);
	//printInInOrder(parent);
	printInLevelOrder(parent);

	for(i = 0; i < out.size(); i++){
		cout << (out[i]);
		if(i != out.size() - 1){
			cout << " ";
		}
	}

	return 0;
}

  






posted on 2012-08-28 13:19  DerDeath  阅读(1146)  评论(0编辑  收藏  举报

导航

"); }); },1000); });