pat 判断给定插入序列是否为BST的先序遍历序列

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

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
//BST Binary Search Tree
//若修改值则*root指向的data可修改
//要修改树形状
//判断给定插入顺序能够构成BST,方法:对构造生成的树进行先序遍历及镜像先序遍历
using namespace std;
struct Node{
	int data;
	Node *lchild;
	Node *rchild;
};
int num;//最后少输出空格
//镜像和非镜像的区别是左右子树的遍历顺序相反,遍历结果录入vector进行比较即可
void pre(Node *root,vector<int> &v){
	if(root==NULL)	return;
	v.push_back(root->data);
	if(root->lchild)	pre(root->lchild,v);
	if(root->rchild)	pre(root->rchild,v);
}

void preM(Node *root,vector<int> &v){
	if(root==NULL)	return;
	v.push_back(root->data);
	if(root->rchild)	preM(root->rchild,v);
	if(root->lchild)	preM(root->lchild,v);	
}

void post(Node *root,vector<int> &v){
	if(root==NULL)	return;
	if(root->lchild)	post(root->lchild,v);
	if(root->rchild)	post(root->rchild,v);
	v.push_back(root->data);
}

void postM(Node *root,vector<int> &v){
	if(root==NULL)	return;
	if(root->rchild)	postM(root->rchild,v);
	if(root->lchild)	postM(root->lchild,v);
	v.push_back(root->data);
}

void insert(Node *&root,int x){//插入到一个不存在的地方
	if(root==NULL){
		root=new Node;
		root->data=x;
		root->lchild=root->rchild=NULL;
	}
	else if(x<root->data){
		insert(root->lchild,x);
	}
	else
		insert(root->rchild,x);
}

int main(){
	vector<int> origin,pres,preMs,posts,postMs;
	int n,temp;
	scanf("%d",&n);
	Node *root=NULL;
	for(int i=0;i<n;i++){
		scanf("%d",&temp);
		origin.push_back(temp);
		insert(root,temp);
	}
	pre(root,pres);
	preM(root,preMs);
	if(pres==origin){
		printf("YES\n");
		post(root,posts);
		int len=posts.size();
		for(int i=0;i<len;i++)
			if(i!=len-1)	printf("%d ",posts[i]);
			else	printf("%d\n",posts[i]);
	}
	else if(preMs==origin){
		printf("YES\n");
		postM(root,postMs);
		int len=postMs.size();
		for(int i=0;i<len;i++)
			if(i!=len-1)	printf("%d ",postMs[i]);
			else	printf("%d\n",postMs[i]);
	}
	else printf("NO\n");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现这个功能,可以采用二叉搜索树的特性,即左子树中所有节点的值小于根节点的值,右子树中所有节点的值大于根节点的值。因此,在先序遍历序列中,根节点的值必定是序列的第一个元素。 具体的实现步骤如下: 1. 定义一个二叉树的结构体,包含键值和左右子节点指针。 2. 定义一个函数,输入先序遍历序列序列长度,返回根节点。 3. 从先序遍历序列中取出第一个元素作为根节点。 4. 遍历序列,将小于根节点值的元素放入左子树序列,大于根节点值的元素放入右子树序列。 5. 递归创建左右子树,并将左右子树的根节点分别挂在根节点的左右子节点上。 6. 采用递归的方式遍历二叉树,查找节点U和V的位置。 7. 如果当前节点为NULL或者等于U或V,则返回当前节点。 8. 如果U和V分别在当前节点的左右子树中,则当前节点为最近公共祖先。 9. 如果U和V在当前节点的同一子树中,则继续向下递归。 10. 最终返回最近公共祖先节点的键值即可。 下面是实现代码的示例: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int key; struct Node* left; struct Node* right; } Node; Node* create_bst(int* preorder, int len) { if (len == 0) { return NULL; } Node* root = (Node*) malloc(sizeof(Node)); root->key = preorder[0]; int i; for (i = 1; i < len; i++) { if (preorder[i] > root->key) { break; } } root->left = create_bst(preorder + 1, i - 1); root->right = create_bst(preorder + i, len - i); return root; } Node* find_lca(Node* root, int u, int v) { if (root == NULL || root->key == u || root->key == v) { return root; } if (u < root->key && v < root->key) { return find_lca(root->left, u, v); } else if (u > root->key && v > root->key) { return find_lca(root->right, u, v); } else { return root; } } int main() { int preorder[] = {6, 2, 1, 4, 3, 5, 9, 7, 10}; int len = sizeof(preorder) / sizeof(preorder[0]); Node* root = create_bst(preorder, len); int u = 3, v = 5; Node* lca = find_lca(root, u, v); printf("LCA of %d and %d is %d\n", u, v, lca->key); u = 4, v = 9; lca = find_lca(root, u, v); printf("LCA of %d and %d is %d\n", u, v, lca->key); u = 4, v = 5; lca = find_lca(root, u, v); printf("LCA of %d and %d is %d\n", u, v, lca->key); return 0; } ``` 输出结果如下: ``` LCA of 3 and 5 is 4 LCA of 4 and 9 is 6 LCA of 4 and 5 is 4 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值