1.1020:
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 7Sample Output:
4 1 6 3 5 7 2
2.1043
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.
Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input 1:7 8 6 5 7 10 8 11Sample Output 1:
YES 5 7 6 8 11 10 8Sample Input 2:
7 8 10 11 8 6 7 5Sample Output 2:
YES 11 8 10 7 5 6 8Sample Input 3:
7 8 6 8 5 10 9 11Sample Output 3:
NO
1020就是给出一个树的后序遍历和中序遍历,让你打印出这个树的层序遍历:
首先是建树阶段,需要找到后序遍历和中序遍历中根节点所在的位置(也就是在后序遍历中的最后一个位置),然后要通过中序遍历找到对应的左子树和右子树(其实就是确定一下中序遍历中根节点所在的位置,左边的就是左子树,右边的就是右子树),并且找出左子树和右子树的长度,然后在根的位置上放上根的值,再递归构建左右子树(注意需要向下传递根节点的位置,也就是2k和2k+1)
#include
#include
using namespace std;
int n;
int k1[10000]={-1};
int k2[10000]={-1};
int tree[10000]={-1};
void build(int pre,int mid,int len,int po){
if(len>0){
int root=k1[pre+len-1];
tree[po]=root;
int i=0;
for(;i>n;
for(int i=0;i>k1[i];
for(int i=0;i>k2[i];
build(0,0,n,1);
queue q;
//q.push(1);
if(tree[2]!=0)
q.push(2);
if(tree[3]!=0)
q.push(3);
cout<
至于1043的话,其实也是建树的时候需要注意一下,每次考虑前序遍历的一个根节点进来的时候,要和当前的节点值比较一下,如果当前节点是个空节点的话,就直接插入,如果不是的话,就和他比较一下,小于的话就往左子树去考虑,大于的话就往右子树考虑,最后在用一下广搜来打印出来。(详见http://blog.csdn.net/douglaslee01/article/details/79446330)点击打开链接
1064
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.
Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.
Sample Input:10 1 2 3 4 5 6 7 8 9 0Sample Output:
6 3 8 1 5 7 9 0 2 4
对于完整二分查找树,有一个重要的性质:树的中遍历==树中元素的递增排列
所以可以根据中序遍历的模式将树构建出来:首先将所有递归找左子树直到找到最左端的节点,然后将当前的值赋给他,然后递归返回上一层,将下一个值赋给它,再考虑右子树。就像下面
#include
#include
using namespace std;
int idx=0;
int tree[1000];
int input[1000];
int n;
void build(int po){//中序遍历就是BST的顺序排列,tree[1]就是根节点
if(po<=n){
build(po<<1);
tree[po]=input[idx++];
build(po<<1|1);
}
}
int main(int argc, const char * argv[]) {
cin>>n;
for(int i=0;i>input[i];
sort(input,input+n);
build(1);
cout<