This topic is to investigate the second query of the search tree, but in fact we do not need to establish a binary tree. We only need to use the recursive idea to directly traverse the tree during the reconstruction process.
Lambda expressions are used in this code, so the amount of code is concise. There are only 40 lines, and there are many interesting features in C ++.
# Include <stdio. h >#include <algorithm> # include <vector> # include <functional> using namespace STD; int N; int A [1005]; vector <int> res; // store the int value of the output result; // temporary variable used for the internal function <int (INT)> func = [=] (int I) of the lambda Function) {return I> = value ;}; // Lambda function pointer void tree (int * a, int * B) {if (a> = B) return; // recursive exit value = * A; int * center = find_if (a + 1, B, func); // The array is divided into three parts: root element left subtree right subtree if (! All_of (center, B, func) // test whether the right subtree meets the condition return; tree (a + 1, center); // traverse the left subtree tree (center, b); // traverse the right subtree res. push_back (* A); // storage output} int main () {scanf ("% d", & N); For (INT I = 0; I <N; I ++) {scanf ("% d", A + I);} If (n> 1 & A [0] <= A [1]) func = ([=] (int I) {return I <value ;}); // if it is mirror, change the lambda function tree (A, A + n ); if (res. size ()! = N) printf ("NO \ n"); else {// output printf ("Yes \ n"); printf ("% d", Res [0]); for (INT I = 1; I <res. size (); I ++) printf ("% d", Res [I]);} return 0 ;}
Pat1043 is it a binary search tree