1043 Is It a Binary Search Tree (determines whether the binary search tree, and after the output order traversal binary search tree)

1043 Is It a Binary Search Tree (25 分)

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

 

This question ideas: 1. must first determine whether the binary search tree.

                  2. If it is the later order traversal output binary search tree

                             Not on no output

method one:

 Violence:               

// c ++ yes -> with the structure.
/ **
binary search tree: smaller than the root node on the left of the root node is greater than or equal to the junction node on the right side

This idea is the question: 1 input into a tree construction preorder traversal mode binary search tree (for loop, each node into the tree) will be placed into a new vector array
            2. preorder Construction preorder traversal of the search tree to tree in question if there is an input compare different nodes, the output "NO", if all the nodes the same (and the same index position)
            of the rear output by a preorder traversal of the search tree nodes

                  
** /

#include <cstdio>
#include<iostream>
#include<vector> 
using namespace std;
int n,i=0;
bool isMirror = false;
vector<int> prel; //存放题目中所给条件树 
vector<int> pre; //存放(二叉搜索)镜像树先序遍历后的结点 
struct Node{
	Node *left;
	Node *right;
	int value;
	Node(int val): value(val), left(nullptr), right(nullptr){} //构造方法,后面不用加; 
};
void findBFS(Node *&root, int value, bool Mirror){  //构建搜索二叉树 
	if(root == nullptr){
		root = new Node(value);
		return;
	}else if(!isMirror && value < root->value){
		findBFS(root->left, value, Mirror);
	}else if(!isMirror && value >= root->value){
		findBFS(root->right, value, Mirror); 
	}else if(isMirror && value < root->value){
		findBFS(root->right, value, Mirror);
	}else if(isMirror && value >= root->value){
		findBFS(root->left, value, Mirror);
	}
}

void preOrder(Node *&root, vector<int> &pre) { //先序遍历搜索二叉树 并将节点放入vertor pre中 
	if(root==nullptr) return;
	pre.push_back(root-> value);
	preOrder(root-> left, pre);
	preOrder(root-> right, pre);
}

void postOrder(Node *&root){  //后序遍历输出搜索二叉树的结点 
	if(root == nullptr) return;
	postOrder(root-> left);
	postOrder(root-> right);
	if(i++ != 0) printf(" ");
	printf("%d",root-> value); 
}

 
int main(){
	Node *root=nullptr; //没有初始化(难受)非全局变量一定要初始化 
	scanf("%d", &n);
	prel.resize(n);
	for(int i = 0;i < n; i++){
		scanf("%d", &prel[i]);
	}
	if(n>1 && prel[1] > prel[0]) isMirror = true;  //prel 不要写为pre  都不会报错的 
	for(int i = 0; i<prel.size(); i++){
		findBFS(root, prel[i], isMirror); // 构建搜索二叉树 
	}
	preOrder(root, pre);
	if(pre!=prel){
		printf("NO");
	}else{
		printf("YES\n");
		postOrder(root); 
	}
	return 0;
}

Method 2 : using a binary tree search Properties

/ **
1025 sequence obtained by the known sequence, the binary tree in preorder traversal of a binary tree of course also be obtained postorder binary
           search tree thus obtained may also be a preorder traversal of the search tree, depending on the nature of the binary tree search can be known search tree
           traversal sequence of numerical values is in ascending order node
           generally needs to know the tree to order, sequence, or after the sequence, sequence specific since the root node can determine the location, as the second root the position of the node is required
           to know the number of left sub-tree nodes, the number of right subtree node
           but binary search tree based on the nature of the binary search tree: Knowing the first traversal, the nodes can determine a left subtree will be less than the root, right subtree nodes are greater than the root,
           and the situation is first traversal of a binary tree would be: a bcd efg (b, c , d <a) (e, f, g> a) Therefore, by continuously recursive can be constructed postorder traversal of the binary tree
           
* /

#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int> pre,post;
bool isMirror,flag=true;
void postOrder(int s, int e){ //
	if(!flag || s > e) return;
	int i=s+1, j=e;
	if(isMirror == false){
		while(i <= e && pre[s] > pre[i]) i++;
		while(j > s && pre[s] <= pre[j]) j--;	
	}else if(isMirror == true){
		while(i <= e && pre[s] <= pre[i]) i++;           //如果不是镜像,比结点大的树放在右边 
		while(j > s && pre[s] > pre[j])  j--;
	}
	if(i-j != 1){ //注意是i - j 因为最后遍历完成索引i在索引j的前面  以 a bcd efg为例子, 
				  // 此时i会指向e ,j会指向d(只要是二叉搜索树,就会满足此性质) 
		flag=false;
		return;              
	} 
	postOrder(s+1, i-1); 
	postOrder(i, e);
	post.push_back(pre[s]);
} 
int main(){
	scanf("%d", &n);		
	pre.resize(n);  //重新设置pre的容量范围,节约时间,直接分配空间大小,vector就不用动态重新构建容器的大小 
	for(int i=0; i<n; i++){
		scanf("%d", &pre[i]);
	}
	if(n > 1 && pre[1] - pre[0] > 0) isMirror=true;	
	postOrder(0, n-1);
	if(!flag)
		printf("NO");
	else{
		printf("YES\n");
		for(auto i = 0; i<post.size(); i++){
			if(i!=0) printf(" "); 
			printf("%d",post[i]);
		} 
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41698081/article/details/90927736