Search tree judgment (25 points) (build binary tree first)

For a binary search tree, we stipulate that the left subtree of any node only contains key values ​​strictly less than the node, and its right subtree contains key values ​​greater than or equal to the node. If we exchange the left subtree and the right subtree of each node, the resulting tree is called a mirrored binary search tree.

Now we give an integer key value sequence, please write a program to determine whether the sequence is a preorder traversal sequence of a binary search tree or a mirrored binary search tree, if so, output the postorder traversal sequence of the corresponding binary tree.

Input format:
The first line of input contains a positive integer N (≤1000), and the second line contains N integers, which are the given integer key value sequence, and the numbers are separated by spaces.

Output format:
The first line of the output first gives the judgment result. If the input sequence is a preorder traversal sequence of a binary search tree or a mirrored binary search tree, it will output YES, and NO will be output. If the judgment result is YES, the next line outputs the post-order traversal sequence corresponding to the binary tree. The numbers are separated by spaces, but there can be no extra spaces at the end of the line.

Insert picture description here
Question idea: The
code is a bit longer, but the idea is still relatively clear

#include<iostream>
using namespace std;

typedef struct tree {
    
    
	int data;
	tree* left;
	tree* right;
} *BinTree;
BinTree BST(int *pre, int len);
BinTree MirBST(int *pre, int len);
void LRD(BinTree T);
int flag1 = 0;
int flag2 = 0;

int ar[1000], k = 0;
int main()
{
    
    
	int preorder[1000];
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> preorder[i];

	BinTree T1 = BST(preorder, n);
	BinTree T2 = MirBST(preorder, n);

	if (T1 && flag1 == 0)
	{
    
    
		cout << "YES\n";
		LRD(T1);
		for (int i = 0; i < n; i++)
		{
    
    
			if (i)cout << " ";
			cout << ar[i];
		}
	}
	else if (T2 && flag2 == 0)
	{
    
    
		cout << "YES\n";
		LRD(T2);
		for (int i = 0; i < n; i++)
		{
    
    
			if (i) cout << " ";
			cout << ar[i];
		}
	}
	else
	{
    
    
		cout << "NO\n";
	}

	return 0;
}
BinTree BST(int *pre, int len)
{
    
    
	if (len == 0)return NULL;
	BinTree T = new tree;
	T->data = *pre;

	int m;
	for ( m = 1; m < len; m++)
		if (pre[m] >= T->data)		//二叉搜索树右侧大于等于T->data,将m调整到T右子树第一个位置
			break;
	int n;
	for (n = m; n < len; n++)
		if (pre[n] < T->data)		//如果T的右子树中有节点的值小于T的data
		{
    
    
			flag1 = 1;				//该二叉树不是二叉搜索树
			return NULL;
		}
	T->left = BST(pre + 1, m - 1);
	T->right = BST(pre + m, len - m);
	return T;
}

BinTree MirBST(int *pre, int len)
{
    
    
	if (len == 0)return NULL;
	BinTree T = new tree;
	T->data = *pre;

	int m;
	for (m = 0; m < len; m++)	//镜像二叉搜索树右侧严格小于T->data,找到第一个右子树的位置
		if (pre[m] < T->data)
			break;

	int n;
	for (n = m; n < len; n++)
		if (pre[n] >= T->data)	//如果镜像二叉树的右子树有一个大于等于T->data的节点
		{
    
    
			flag2 = 1;			//该镜像二叉树不是镜像搜索二叉树
			return NULL;
		}

	T->left = MirBST(pre + 1, m - 1);
	T->right = MirBST(pre + m, len - m);
	return T;
}
void LRD(BinTree T)
{
    
    
	if (T)
	{
    
    
		LRD(T->left);
		LRD(T->right);
		ar[k++] = T->data;
	}
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/113988615