1043. is It a Binary Search Tree (25)

Source: Internet
Author: User

The topics are as follows:

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:
78 6 5 7 10 8 11
Sample Output 1:
YES5 7 6 8 11 10 8
Sample Input 2:
78 10 11 8 6 7 5
Sample Output 2:
YES11 8 10 7 5 6 8
Sample Input 3:
78 6 8 5 10 9 11
Sample Output 3:
NO

The narrative of this topic seems to have some problems, when the problem is determined that the given sequence of the image of the first sequence of BST, should output the sequential sequence of the mirror BST.

The first step is to set up BST, the method is simple, first define a node is empty to store the root node, define an insert method, when T is empty to establish the node, when T is not empty, according to the BST conditions T->left and t->right recursive.

void Insert (Node &t,int num) {    if (! T) {        t = new TreeNode ();        T->data = num;        return;    }    if (num < t->data) {        insert (t->left,num);    } else{        Insert (t->right,num);}    }
It is important to note that the node parameter should be passed into the reference, so add a &amp, otherwise you cannot directly manipulate the T parameter to the outside.

For the entire input sequence, the N-Times insert method is called to get the complete BST, and the root node is T.


The following only requires a pre-order traversal of BST and a mirror pre-sequence traversal (root right-left), to see if the problem gives the sequence and which match, can determine whether it is BST or MBST, if not the output No.

The complete code is as follows:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <vector>using namespace std    ; typedef struct TREENODE* node;struct treenode{Node left;    Node right;    int data;    TreeNode () {left = right = NULL; }};int *pre;int n;vector<int> preorderv;vector<int> mpreorderv;vector<int> result;void Insert (Node &t,int num) {if (!        T) {t = new TreeNode ();        T->data = num;    Return    } if (num < t->data) {insert (t->left,num);    }else{Insert (T->right,num); }}void Preorder (Node T) {if (!    T) return;    Preorderv.push_back (T->data);    Preorder (T->left); Preorder (t->right);} void Postorder (Node T) {if (!    T) return;    Postorder (T->left);    Postorder (T->right); Result.push_back (t->data);} void Mbstpreorder (Node T) {if (!    T) return;    Mpreorderv.push_back (T->data);    Mbstpreorder (T->right); Mbstpreorder (t->left);} BOOL Arrayequal2vector (int *Arr, vector<int> Vec) {for (int i = 0; i < vec.size (); i++) {if (Arr[i]! = Vec[i]) return false; } return true; void Mirror (Node T) {if (!    T) return;    Node temp = t->left;    T->left = t->right;    T->right = temp;    Mirror (T->left); Mirror (t->right);}    int main () {cin >> N;    Pre = (int*) malloc (sizeof (int) *n);    int num;    Node T = NULL;        for (int i = 0; i < N; i++) {scanf ("%d", &num);        Pre[i] = num;    Insert (T,num);    } preorderv.clear ();    Mpreorderv.clear ();    Result.clear ();    Preorder (T);    Mbstpreorder (T);        if (Arrayequal2vector (Pre,preorderv)) {cout << "YES" << Endl;    Postorder (T);        }else if (Arrayequal2vector (Pre,mpreorderv)) {cout << "YES" << Endl;        Mirror (T);    Postorder (T);    }else{cout << "NO" << Endl;        } if (Result.size ()! = 0) {cout << result[0]; for (int i = 1; i < result.size (); i++) printf ("%d", result[i]);    cout << Endl; } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

1043. is It 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.