l2-004. Is this a two-fork search tree?

Source: Internet
Author: User

l2-004. Is this a two-fork search tree? Time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen

A binary search tree can be recursively defined as a two-fork tree with the following properties: for any node,

    • The key value of all nodes in the left subtree is less than the key value of the node;
    • The key value of all nodes in the right subtree is greater than or equal to the key value of the node;
    • The left and right sub-trees are two-fork search trees.

The "mirror" of the so-called binary search tree is the tree that is obtained after the left and right sub-trees of all the nodes are swapped for positions.

Given a sequence of integer key values, you are now asked to write a program that determines whether this is the result of a pre-order traversal of a binary search tree or its image.

Input format:

The first line of the input gives the positive integer n (<=1000). The next line gives an n integer key value, separated by a space.

Output format:

If the input sequence is the result of a pre-order traversal of a binary search tree or its image, first output "YES" in a row and then output the result of the tree's post-ordering traversal on the next line. There are 1 spaces between the numbers, and there must be no extra spaces at the end of each line. If the answer is NO, then output "NO".

Input Sample 1:
78 6 5 7 10 8 11
Output Example 1:
YES5 7 6 8 11 10 8
Input Sample 2:
78 10 11 8 6 7 5
Output Example 2:
YES11 8 10 7 5 6 8
Input Sample 3:
78 6 8 5 10 9 11
Output Example 3:
NO
Ideas: We only need to refine test instructions a few key words, first of all, according to the binary search tree three characteristics of the achievements,
Also by the way, The Mirror tree (originally left on the right side) written out,
Then it compares the sequence that the forward sequence traversal is given, and it is the sequence of the output.
1#include <bits/stdc++.h>2 using namespacestd;3 //Two fork Search tree features: The left node is smaller than the root node, the right node is larger than the root node4 structnode{5     intdata;6     structNode *l;7     structNode *R;8 };9 ints[1010],v[1010],tmp;Ten voidCreate1 (Node *root,intx) {//Achievements OneNode *p; A     if(x<root->data) { -             if(root->l==NULL) { -p=NewNode; theP->data=x; -P->l=NULL; -P->r=NULL; -Root->l=p; +                 return ; -             } +             Else  ACreate1 (root->l,x); at     } -     Else{ -         if(root->r==NULL) { -p=NewNode; -P->data=x; -P->l=NULL; inP->r=NULL; -Root->r=p; to             return ; +         } -         Else theCreate1 (root->r,x); *     } $ }Panax Notoginseng voidCreate2 (Node *root,intx) {//Mirror Tree -Node *p; the     if(x>=root->data) { +             if(root->l==NULL) { Ap=NewNode; theP->data=x; +P->l=NULL; -P->r=NULL; $Root->l=p; $                 return ; -             } -             Else  theCreate2 (root->l,x); -     }Wuyi     Else{ the         if(root->r==NULL) { -p=NewNode; WuP->data=x; -P->l=NULL; AboutP->r=NULL; $Root->r=p; -             return ; -         } -         Else ACreate2 (root->r,x); +     } the } - voidFirstvisit (Node *p) {//Pre-sequence traversal: root around $     if(p!=NULL) { theV[tmp++]=p->data; theFirstvisit (p->l); theFirstvisit (p->R); the     } - } in voidLastvisit (Node *p) {//Post-post traversal: Left and right root the     if(p!=NULL) { theLastvisit (p->l); AboutLastvisit (p->R); theV[tmp++]=p->data; the     } the } + intMain () { -     intN,flag; theNode *root,*Toor;Bayiroot=NULL; thescanf"%d",&n); the      for(intI=0; i<n;i++){ -scanf"%d",&s[i]); -         if(i==0)//root node the         { theroot=NewNode; theRoot->data=S[i]; theRoot->l=NULL; -Root->r=NULL; theToor=NewNode; theToor->data=S[i]; theToor->l=NULL;94Toor->r=NULL; the         } the         Else{ the create1 (Root,s[i]);98 Create2 (Toor,s[i]); About         } -     }101tmp=0;102flag=1;103 firstvisit (root);104      for(intI=0; i<n;i++){ the         if(s[i]!=V[i]) {106flag=0;107              Break;108         }109     } the     if(flag==1){111printf"yes\n"); thetmp=0;113 lastvisit (root); theprintf"%d", v[0]); the          for(intI=1; i<n;i++) theprintf"%d", V[i]);117         118     }119     Else{ -tmp=0;121flag=1;122 firstvisit (toor);123          for(intI=0; i<n;i++){124             if(s[i]!=V[i]) { theflag=0;126                  Break;127             } -         }129         if(flag==1){ theprintf"yes\n");131tmp=0; the lastvisit (toor);133printf"%d", v[0]);134              for(intI=1; i<n;i++)135printf"%d", V[i]);136         }137         Else138printf"no\n");139     } $     return 0;141}

l2-004. Is this a two-fork 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.