1043. is It a binary Search tree (25) "Binary Tree" (--pat) practise

Source: Internet
Author: User

Topic Information

1043. is It a Binary Search Tree (25)

Time limit MS
Memory Limit 65536 KB
Code length limit 16000 B

A binary Search Tree (BST) is recursively defined as a Binary Tree which have 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 is 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 the integers keys, you were supposed to the if it is the preorder traversal sequence of a BST or the MI Rror image of a BST.

Input Specification:

Each input file contains the one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys is given in the next line. All the numbers in a line is separated by a space.

Output Specification:

For each test case, first print with 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 are "YES", print in the next line the Postorder traversal sequence of that tree. All the numbers in a line must is separated by a space, and there must is 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

Thinking of solving problems

Establish two trees in maximum and minimum order, and then compare the consistency of the previous sequence traversal with the data. Pay attention to the symmetry when comparing the achievements

AC Code
#include <cstdio>#include <vector>#include <algorithm>#include <cstring>using namespace STD;structnode{Node *left, *right;intV;}; node* Create (intV) {Node *h =NewNode ();    H->left = H->right = NULL; H->v = v;returnH;} node* Insert1 (Node *root,intV) {if(Root = NULL) {returnCreate (v); }Else{if(v < root->v)        {root->left = Insert1 (Root->left, v); }Else{root->right = Insert1 (Root->right, v); }    }returnRoot;} node* Insert2 (Node *root,intV) {if(Root = NULL) {returnCreate (v); }Else{if(v >= root->v)        {root->left = Insert2 (Root->left, v); }Else{root->right = Insert2 (Root->right, v); }    }returnRoot;}voidPreorder (node* root, vector<int>&AMP;V) {v.push_back (ROOT-&GT;V);if(root->left) Preorder (Root->left, v);if(root->right) Preorder (root->right, v);}voidPostorder (node* root, vector<int>&AMP;V) {if(root->left) Postorder (Root->left, v);if(root->right) Postorder (Root->right, v); V.push_back (ROOT-&GT;V);}intMain () {intN, T;scanf("%d", &n); vector<int>V, V1, v2, RS; Node *t1 = null, *T2 = NULL; for(inti =0; I < n; ++i) {scanf("%d", &t);        V.push_back (t);        T1 = insert1 (t1, T);    t2 = Insert2 (t2, T);    } preorder (t1, v1); Preorder (T2, V2);if(v = = v1) {printf("yes\n"); Postorder (t1, RS);printf("%d", rs[0]); for(inti =1; I < rs.size (); ++i) {printf("%d", Rs[i]); }    }Else if(v = = v2) {printf("yes\n"); Postorder (T2, RS);printf("%d", rs[0]); for(inti =1; I < rs.size (); ++i) {printf("%d", Rs[i]); }    }Else{printf("NO"); }printf("\ n");return 0;}

1043. is It a binary Search tree (25) "Binary Tree" (--pat) practise

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.