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.
Analysis: This question is to give a series of numbers, either bst or bst mirror image.
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
/*
Preface: 1. Accessing Root Nodes
2.Accessing the left subtree
3.Accessing the right subtree
Postorder: 1. Accessing the left subtree
2.Accessing the right subtree
3.Access root
So the bst prefix is the root node, and then gradually decreases until the right node becomes larger.
bst Post-order algorithm is from small to big to root, and then from big to small.
Then the precedence of the mirror image of bst is the root node, and then gradually increases until the right node gradually decreases.
Post-order algorithm is from big to small to root, and then from small to big.
*/
#include <cstdio>
#include <vector>
using namespace std;
bool isMirror;
vector<int> v1;
vector<int> v2;
void getpost (int root , int tail)
{
if (root > tail)
return;
int i = root + 1 , j = tail;
//Judge if it's a mirror image?
if (!isMirror)
{
//Finding from the left is smaller than the root node value
while (i <= tail && v1[root] > v1[i])
i++;
//Find from the right that is larger or equal to the root node value
while (j > root && v1[root] <= v1[j])
j--;
}
else//Mirrors are the other way around.
{
while (i <= tail && v1[root] <= v1[i])
i++;
while (j > root && v1[root] > v1[j])
j--;
}
//i j is not adjacent to exit
if (i - j != 1)
return;
/*
1,First, we construct the right sub-number of the mirror image, that is, from small to root.
If the root node is removed, then all the trees smaller than the root node are left subtrees, so we can make a new judgement.
If it's bst, then root + 1 > tail exits
The smallest value is root, where root=tail
Similarly, the right subtree exits directly and enters the minimum value into v2.
2,Input root node
3,Finally, we construct the left subtree of the mirror image, that is, from the root to the large, when root+1 is always greater than j.
It's all about judging the number of right children directly until you find the maximum exit.
*/
getpost (root + 1 , j);
getpost (i , tail);
v2.push_back (v1[root]);
}
int main ()
{
int n;
scanf_s ("%d" , &n);
v1.resize (n);
for (int i = 0; i < n; i++)
scanf_s ("%d" , &v1[i]);
getpost (0 , n - 1);
if (v2.size () != n)
{
isMirror = true;
v2.clear ();
getpost (0 , n - 1);
}
if (v2.size () == n)
{
printf ("YES\n%d" , v2[0]);
for (int i = 1; i < n; i++)
printf (" %d" , v2[i]);
}
else
{
printf ("NO");
}
system ("pause");
return 0;
}
Summary: At one time, I thought that the total root was left and right all the time. I didn't understand the output for half a day. It's silly. Alas, every subtree is also a bst. It can be solved from the smallest subtree to the root, but I may have to recite the algorithm....