Pat1043 is it a binary search tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.