您的位置:首页 > 其它

1043. Is It a Binary Search Tree

2018-03-05 20:30 232 查看

Is It a Binary Search Tree

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


题意

给定n个正整数,判断其是否是二叉查找树或镜像二叉查找树的先序序列(镜像二叉查找树是将二叉查找树所有结点的左右子树互换得到的),若是则列出其后序序列。

注意题目里定义的二叉查找树是左子树均小于根节点,而右子树均大于等于根节点。

思路

由给定的n个正整数来重建二叉查找树,并记录(镜像)二叉查找树的先序序列、后序序列,与给定的先序序列进行比较,若相同则输出后序序列。

镜像二叉查找树的的遍历只要在原树遍历时交换左右子树的访问顺序即可。

内置数据类型的vector变量可直接比较大小。

代码实现

#include <cstdio>
#include <vector>
using namespace std;

vector<int> ini, pre, post, preM, postM;

struct node
{
int data;
struct node* lchild;
struct node* rchild;
};

void insert(node* &root, int x)     // 二叉树插入结点
{
if (root == NULL)
{
root = new node;
root->data = x;
root->lchild = root->rchild = NULL;
return;
}

if (x >= root->data)
insert(root->rchild, x);
else
insert(root->lchild, x);
}

void preOrder(node* root)       // 原树先序遍历
{
if (root == NULL)
return;

pre.push_back(root->data);
preOrder(root->lchild);
preOrder(root->rchild);
}

void postOrder(node* root)      // 原树后序遍历
{
if (root == NULL)
return;

postOrder(root->lchild);
postOrder(root->rchild);
post.push_back(root->data);
}

void preOrderM(node* root)      // 镜像树先序遍历
{
if (root == NULL)
return;

preM.push_back(root->data);
preOrderM(root->rchild);
preOrderM(root->lchild);
}

void postOrderM(node* root)     // 镜像树后序遍历
{
if (root == NULL)
return;

postOrderM(root->rchild);
postOrderM(root->lchild);
postM.push_back(root->data);
}

int main()
{
int n, x;
node* root = NULL;

scanf("%d", &n);
for (int i = 0; i < n; i++)     // 二叉树构建
{
scanf("%d", &x);
ini.push_back(x);
insert(root, x);
}

preOrder(root);
postOrder(root);
preOrderM(root);
postOrderM(root);

if (ini == pre)     // 先序序列与原树一致
{
printf("YES\n");
for (int i = 0; i < post.size(); i++)
{
if (i > 0)
printf(" ");
printf("%d", post[i]);
}
}
else if (ini == preM)   // 先序序列与镜像树一致
{
printf("YES\n");
for (int i = 0; i < postM.size(); i++)
{
if (i > 0)
printf(" ");
printf("%d", postM[i]);
}
}
else

4000
printf("NO\n");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息