与树相关的pat题目

1.1020:

1020. Tree Traversals (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

2.1043

1043. Is It a Binary Search Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

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 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:

NO

1020就是给出一个树的后序遍历和中序遍历,让你打印出这个树的层序遍历:

首先是建树阶段,需要找到后序遍历和中序遍历中根节点所在的位置(也就是在后序遍历中的最后一个位置),然后要通过中序遍历找到对应的左子树和右子树(其实就是确定一下中序遍历中根节点所在的位置,左边的就是左子树,右边的就是右子树),并且找出左子树和右子树的长度,然后在根的位置上放上根的值,再递归构建左右子树(注意需要向下传递根节点的位置,也就是2k和2k+1)

#include <iostream>
#include<queue>
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<len;i++){
        if(k2[mid+i]==root)
            break;
    }
    build(pre,mid,i,2*po);
    build(pre+i,mid+i+1,len-1-i,2*po+1);
    }
}
int main(int argc, const char * argv[]) {
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>k1[i];
    for(int i=0;i<n;i++)
        cin>>k2[i];
    build(0,0,n,1);
    queue<int> q;
    //q.push(1);
    if(tree[2]!=0)
        q.push(2);
    if(tree[3]!=0)
        q.push(3);
    cout<<tree[1];
    while(!q.empty()){
        int k=q.front();
        cout<<' '<<tree[k];
        q.pop();
        if(tree[2*k]!=0)
            q.push(2*k);
        if(tree[2*k+1]!=0)
            q.push(2*k+1);
    }
    
    
    return 0;
}


至于1043的话,其实也是建树的时候需要注意一下,每次考虑前序遍历的一个根节点进来的时候,要和当前的节点值比较一下,如果当前节点是个空节点的话,就直接插入,如果不是的话,就和他比较一下,小于的话就往左子树去考虑,大于的话就往右子树考虑,最后在用一下广搜来打印出来。(详见http://blog.csdn.net/douglaslee01/article/details/79446330)点击打开链接

1064

1064. Complete Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

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 0
Sample Output:
6 3 8 1 5 7 9 0 2 4

对于完整二分查找树,有一个重要的性质:树的中遍历==树中元素的递增排列

所以可以根据中序遍历的模式将树构建出来:首先将所有递归找左子树直到找到最左端的节点,然后将当前的值赋给他,然后递归返回上一层,将下一个值赋给它,再考虑右子树。就像下面

#include <iostream>
#include<algorithm>
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<n;i++)
        cin>>input[i];
    sort(input,input+n);
    build(1);
    cout<<tree[1];
    for(int i=2;i<=n;i++)
        cout<<' '<<tree[i];
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值