Binary sort Tree

Source: Internet
Author: User

Time limit:1000ms Memory limit:65536k Title Description

The binary sort tree is defined as either an empty tree or a two-fork tree with the following properties: If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node, and if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node, and its left and right subtrees are also two-fork sorting trees. Today we are going to judge whether the two sequences are the same one or two-fork sort tree

The input begins with a number n, (1<=n<=20) indicates that there are n needs to be judged, and the input ends when n= 0. The next line is a sequence, the sequence length is less than 10, contains (0~9) number, there is no repetition number, according to this sequence can construct a binary sorting tree. The next n rows have n sequences, and each sequence format is the same as the first sequence, so please determine whether the two sequences can form the same binary sort tree. (Data guarantee does not have empty tree) output sample input
21234567899876543214321567890
Sample output
NONO
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < deque> #include <stack> #include <map> #include <set> #define LL long long#define MAXN 1010#define pp pair<int,int> #define INF 0x3f3f3f3f#define max (x x > (y))? (x): (y)) #define min (x, y) ((() > (y))? (y): (x)) using namespace std;typedef struct Node{char d;node *l,*r;} *p;int n,sb;void Insert (P &t,int x) {if (t==null) {t=new node; t->l=null; t->r=null; T->d=x;} Else{if (x<t->d) Insert (t->l,x); Elseinsert (t->r,x);}} void Dfs (P T,char *ans) {if (T) {Ans[sb++]=t->d;dfs (T->l,ans);d FS (T->r,ans);}} int main () {while (~SCANF ("%d", &n) &&n) {p Root=null;char tem[12],ans[12];scanf ('%s ', TEM); for (int i=0;i <strlen (TEM); i++) Insert (Root,tem[i]), Sb=0;dfs (Root,ans); ans[sb]= ' + ', while (n--) {p troot=null;scanf ("%s ", TEM), for (int i=0;i<strlen (TEM); i++) Insert (Troot,tem[i]);    Sb=0;dfs (Troot,tem); tem[sb]= '; if (!strcmp (Ans,tem)) puts ("YES"); Elseputs ("NO");}} return 0;}

Two fork sort 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.