1043. Is It a Binary Search Tree (25)

来源:互联网 发布:阿里云开通码 编辑:程序博客网 时间:2024/06/13 16:51
#include<iostream>#include<cstdlib>//    6//  5  7//    6//     6using namespace std;struct Node{    Node* left;    Node* right;    int value;};int a[1001];int N;Node* createTree(int first,int last){    if(last-first<0){        return NULL;    }    Node* root = (Node*)malloc(sizeof(Node));    root->value = a[first];    root->left = NULL;    root->right = NULL;    if(first == last){        //cout << root->value << endl;        return root;    }    int i = first+1;    if(a[i] < a[first]){        for(; i <= last;i++){            if(a[i] >= a[first]){                break;            }        }        //cout << i << endl;        root->left = createTree(first+1,i-1);        root->right = createTree(i,last);    }else{        for(; i <= last;i++){            if(a[i] < a[first]){                break;            }        }        //cout << i << endl;        root->right = createTree(first+1,i-1);        root->left = createTree(i,last);    }    //root->left = createTree(first+1,i-1);    //root->right = createTree(i,last);    return root;}bool first = true;void visit(int value){    if(first == true){        cout << value;        first = false;    }else{        cout << ' ' << value ;    }}int pre1[1001];int index1 = 0;void preOrder1(Node* root){    pre1[index1++] = root->value;    if(root->left != NULL){        preOrder1(root->left);    }    if(root->right != NULL){        preOrder1(root->right);    }}int pre2[1001];int index2 = 0;void preOrder2(Node* root){    pre2[index2++] = root->value;    if(root->right != NULL){        preOrder2(root->right);    }    if(root->left != NULL){        preOrder2(root->left);    }}void postOrder1(Node* root){    if(root->left != NULL){        postOrder1(root->left);    }    if(root->right != NULL){        postOrder1(root->right);    }    visit(root->value);}void postOrder2(Node* root){    if(root->right != NULL){        postOrder2(root->right);    }if(root->left != NULL){        postOrder2(root->left);    }    visit(root->value);}int in1[1001];int index3 = 0;void inOrder1(Node* root){    if(root->left != NULL){        inOrder1(root->left);    }    in1[index3++] = root->value;    if(root->right != NULL){        inOrder1(root->right);    }}int in2[1001];int index4 = 0;void inOrder2(Node* root){    if(root->right != NULL){        inOrder2(root->right);    }    in2[index4++] = root->value;    if(root->left != NULL){        inOrder2(root->left);    }}bool isBST(Node* root){    inOrder1(root);    //cout << endl;    inOrder2(root);    //cout << endl;    bool isbst1 = true;    for(int i = 1 ; i < N;i++){        if(in1[i-1] > in1[i]){            isbst1 = false;            break;        }    }    bool isbst2 = true;    for(int i = 1 ; i < N;i++){        if(in2[i-1] > in2[i]){            isbst2 = false;            break;        }    }    return isbst1 || isbst2;   // return isbst1;}int main(){    cin >> N;    for(int i = 0;i < N;i++){        cin >> a[i];    }    Node* root = createTree(0,N-1);    if(!isBST(root)){        cout << "NO" << endl;        return 0;    }    bool flag1 = true;    bool flag2 = true;    preOrder1(root);    for(int i = 0; i < N;i++){        if(pre1[i] != a[i]){            flag1 = false;            break;        }    }    preOrder2(root);    for(int i = 0; i < N;i++){        if(pre2[i] != a[i]){            flag2 = false;            break;        }    }    if(flag1 == true){        cout << "YES" << endl;        postOrder1(root);    }else if(flag2 == true){        cout << "YES" << endl;        postOrder2(root);    }else{        cout << "NO" << endl;    }    return 0;}
创建排序二叉树的第二种方式
<pre name="code" class="cpp">void insert(Node* &root, int value){    if(root = null){        root = new Node;        root->value = value;        root->left = root->right = NULL;        return;    }    if(value < root->value){        insert(root->left,value);    }else{        insert(root->right,value);    }}



0 0
原创粉丝点击