天天看点

⭐⭐⭐⭐⭐PAT A1119 Pre- and Post-order Traversals题目描述知识点实现二刷代码

文章目录

  • 题目描述
  • 知识点
  • 实现
    • 码前思考
    • 代码实现
    • 码后反思
  • 二刷代码

题目描述

⭐⭐⭐⭐⭐PAT A1119 Pre- and Post-order Traversals题目描述知识点实现二刷代码
⭐⭐⭐⭐⭐PAT A1119 Pre- and Post-order Traversals题目描述知识点实现二刷代码

知识点

二叉树的遍历

实现

码前思考

  1. 首先要理解为什么前序遍历和后序遍历的结果有可能不能构成唯一一颗树?

    考虑下面两种情形:

    1.
    pre:  1、2 ...
    post: ... 3、1
    2.
    pre:  1、2 ...
    post: ... 2、1
               

    对于1,我们很容易知道结点1的左子树是以2为根结点,右子树是以3为根结点。

    但是对于2,结点1指用一棵子树,我们无法判断2是1的左节点还是右结点。

    以上,就是为什么前序遍历结果和后序遍历结果可能无法得到唯一树的原因。

  2. 结合1中的讨论,我们很快就能有思路了,参考的模板应该是《算法笔记》上根据前序遍历和中序遍历结果得到的树的那份代码模板。
  3. 需要注意的是,题中既然说明了肯定会有结果,那么就不会存在很多不能构成树的情况,减轻了我们的代码思维量,但是还是要好好思考清楚才是。

代码实现

//使用递归函数来解决这个问题 
#include "bits/stdc++.h"
using namespace std;

const int maxn = 50;

bool flag;
int n; 
int pre[maxn];
int post[maxn];
int in[maxn];
int cnt;

struct node{
	int val;
	node* l;
	node* r;
	//构造函数 
	node(int v):val(v),l(NULL),r(NULL){}
}; 

//函数用于返回[preL,preR]和[postL,postR]内的根结点的函数 
node* create(int preL,int preR,int postL,int postR){
	//首先生成这个结点 
	node* root = new node(pre[preL]); 
	//如果当前子树的长度不为1 
	if(preL != preR){
		//那么判断所谓的左子树和右子树是否相等
		if(pre[preL+1] == post[postR-1]){
			//说明遇到相等的点了
			flag = true;
			//因为题中只要求输出一种,所以就假定是右子树! 
			root->l = NULL;
			root->r = create(preL+1,preR,postL,postR-1);
		}else{
			//不相等,说明一定能够确定左右子树,那么需要找左子树和右子树
			//左子树的长度
			int numLeft=0;
			for(int i=preL+1;i<=preR;i++){
				if(pre[i] != post[postR-1]){
					numLeft++;
				}else{
					break;
				}
			}		
			//得到左子树的长度之后,直接进行递归
			root->l = create(preL+1,preL+numLeft,postL,postL+numLeft-1);
			root->r = create(preL+numLeft+1,preR,postL+numLeft,postR-1); 
		}
		return root; 
	}else{
		//说明这个是单个节点了,没啥操作,直接返回
		return root; 
	} 

}

void inOrder(node* root){
	if(root == NULL){
		return;
	}else{
		inOrder(root->l);
		in[cnt++] = root->val;
		inOrder(root->r);
		return;
	}
} 

int main(){
	cnt = 0;
	flag = false;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&pre[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&post[i]);
	} 
	
	//开始进行构树,使用递归进行
	//树的根结点
	node* root = NULL;
	root = create(0,n-1,0,n-1); 
	
	if(flag == true){
		printf("No\n");
	}else{
		printf("Yes\n");
	}
	
	//进行中序遍历
	inOrder(root);
	for(int i=0;i<n;i++){
		printf("%d",in[i]);
		if(i!=n-1){
			printf(" ");
		}else{
			printf("\n");
		}
	} 
	return 0;
}
           

码后反思

  1. 柳神的简洁代码:
    #include <iostream>
    #include <vector>
    using namespace std;
    vector<int> in, pre, post;
    bool unique = true;
    void getIn(int preLeft, int preRight, int postLeft, int postRight) {
        if(preLeft == preRight) {
            in.push_back(pre[preLeft]);
            return;
        }
        if (pre[preLeft] == post[postRight]) {
            int i = preLeft + 1;
            while (i <= preRight && pre[i] != post[postRight-1]) i++;
            if (i - preLeft > 1)
                getIn(preLeft + 1, i - 1, postLeft, postLeft + (i - preLeft - 1) - 1);
            else
                unique = false;
            in.push_back(post[postRight]);
            getIn(i, preRight, postLeft + (i - preLeft - 1), postRight - 1);
        }
    }
    int main() {
        int n;
        scanf("%d", &n);
        pre.resize(n), post.resize(n);
        for (int i = 0; i < n; i++)
            scanf("%d", &pre[i]);
        for (int i = 0; i < n; i++)
            scanf("%d", &post[i]);
        getIn(0, n-1, 0, n-1);
        printf("%s\n%d", unique == true ? "Yes" : "No", in[0]);
        for (int i = 1; i < in.size(); i++)
            printf(" %d", in[i]);
        printf("\n");
        return 0;
    }
               
  2. 柳神是直接在建树的过程中就得到中序遍历结果了,赞👍
  3. 柳神都没有使用结构体,直接用的数组,赞👍
  4. 一个很奇怪的小问题,我之前在

    create

    函数里忘记写倒数第二个

    return root

    了,但是我本地居然没报错,能够运行成功!我提交到PAT就错了,害的我差点怀疑人生!好迷呀。所以以后写函数要时刻注意返回值的书写!
  5. 还好在最后15分钟检查出来了,不然PAT考零分了!

二刷代码

  1. 已经快自觉养成不写结构体的习惯了。。。
  2. 我这里还顺便统计了一下能够形成多少棵子树。(这个就是南大去年夏令营的题目啊~)
//模仿南大,求数量 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 50;

int n;
int cnt=0; 
int pre[maxn];
int post[maxn];
vector<int> in; 
bool flag=true;

int create(int preL,int preR,int postL,int postR){
	if(preL == preR){//说明是一个叶子节点
		//直接放入
		in.push_back(pre[preL]);
		return 1;
	}else{
		//如果有左子树/右子树,或者都有
		//首先判断是否两个子树都有?
		if(pre[preL+1]!=post[postR-1]){
			//说明存在两棵子树
			//寻找左子树的长度 
			int k;
			for(k=preL+1;k<=preR;k++){
				if(pre[k]==post[postR-1]){
					break;
				}
			}
			//左子树的长度
			int numLeft = k-preL-1;
			
			int leftCnt = create(preL+1,preL+numLeft,postL,postL+numLeft-1);
			in.push_back(pre[preL]);
			int rightCnt = create(preL+numLeft+1,preR,postL+numLeft,postR-1);
			return leftCnt*rightCnt;
		}else{
			//说明只有一棵子树,那么就不唯一了
			flag=false;
			//默认为右子树吧
			in.push_back(pre[preL]);
			int rightCnt = create(preL+1,preR,postL,postR-1); 
			return 2*rightCnt;//需要乘以两倍 
		} 
	} 
}

int main(){
	freopen("input.txt","r",stdin);
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&pre[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&post[i]);
	}
	
	//开始尝试构建树
	cnt = create(0,n-1,0,n-1); 
	printf("cnt:%d\n",cnt); 
	
	if(flag==true){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
	
	for(int i=0;i<n;i++){
		printf("%d%c",in[i],i!=in.size()-1?' ':'\n');
	} 
	
	return 0;
} 
           

继续阅读