pat04-Tree 7. Search in a Binary search Tree (25)

Source: Internet
Author: User

04-Tree 7. Search in a Binary search Tree (25) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

To search a key in a binary search tree, we start from the root and move all the choosing branches according to The comparison results of the keys. The searching path corresponds to a sequence of keys. For example, following {1, 4, 2, 3} We can find 3 from a binary search tree with 1 as its root. But {2, 4, 1, 3} are not such a path since 1 are in the right subtree of the root 2, which breaks the rule for a binary sear Ch Tree. Now given a sequence of keys, you is supposed to tell whether or not it indeed correspnds to a searching path in a binary Search tree.

Input Specification:

Each input file contains the one test case. The first line gives positive integers N and M (<=100) which is the total number of sequences, and The size of each sequence, respectively. Then N lines follow, each gives a sequence of keys. It is assumed, the keys numbered from 1 to M.

Output Specification:

For each sequence, print with a line "YES" if the sequence does correspnd to a searching path in a binary search tree, or "N O "if not.

Sample Input:
3 41 4 2 32 4 1 33 2 4 1
Sample Output:
Yesnono

Submit Code

Law One:

If it is a two-fork search tree, in addition to the leaf nodes in the tree, each node of the left and right sub-tree must have one empty, the other tree is not empty.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7 using namespacestd;8 structnode{9     intv;TenNode *l,*R; One node () { AL=r=NULL; -     } - }; the intMain () { -     //freopen ("D:\\input.txt", "R", stdin); -     intn,m; -scanf"%d%d",&n,&m); +      while(n--){ -queue<int>Q; +         intI,num; ANode *p,*pp,*h=NULL; at          for(i=0; i<m;i++){ -scanf"%d",&num); - Q.push (num); -         } -H=Newnode (); -h->v=Q.front (); in Q.pop (); -          for(i=1; i<m;i++){ tonum=Q.front (); + Q.pop (); -p=h; the              while(p) { *Pp=p;//pp point to the father of P $                 if(num>p->v&&p->l==NULL) {Panax NotoginsengP=p->R; -                 } the                 Else{ +                     if(num<p->v&&p->r==NULL) { AP=p->l; the                     } +                     Else -                          Break;//prevent elements from being equal $                 } $             } -             if(p==NULL) { -p=Newnode (); thep->v=num; -                 if(num>pp->v) {WuyiPp->r=p; the                 } -                 Else{ WuPp->l=p; -                 } About             } $             Else{ -                  Break; -             } -         } A         if(i==m) { +printf"yes\n"); the         } -         Else{ $printf"no\n"); the         } the     } the     return 0; the}

Law II:

First, according to test instructions achievements, get the tree, with the last number of input to the tree traversal, if the last m times to find a matching leaf node, the sequence is correct; otherwise, the sequence is wrong.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7 using namespacestd;8 structnode{9     intv;TenNode *l,*R; One node () { AL=r=NULL; -     } - }; the intMain () { -     //freopen ("D:\\input.txt", "R", stdin); -     intn,m; -scanf"%d%d",&n,&m); +      while(n--){ -queue<int>Q; +         intI,num,wantfind; ANode *p,*pp,*h; at          for(i=0; i<m;i++){ -scanf"%d",&num); - Q.push (num); -         } -Wantfind=num;//last one -H=Newnode (); inh->v=Q.front (); - Q.pop (); to          for(i=1; i<m;i++) {//Achievements +num=Q.front (); - Q.pop (); thep=h; *              while(p) { $Pp=p;//pp point to the father of PPanax Notoginseng                 if(num>p->v) { -P=p->R; the                 } +                 Else{ AP=p->l; the                 } +             } -p=Newnode (); $p->v=num; $             if(num>pp->v) { -Pp->r=p; -             } the             Else{ -Pp->l=p;Wuyi             } the         } -         intCount=0; Wup=h; -          while(p) { About             if(wantfind>p->v) { $P=p->R; -             } -             Else{ -P=p->l; A             } +count++; the         } -         if(count==L) { $printf"yes\n"); the         } the         Else{ theprintf"no\n"); the         } -     } in     return 0; the}

pat04-Tree 7. Search in a Binary search Tree (25)

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.