zoukankan      html  css  js  c++  java
  • PAT-1043. Is It a Binary Search Tree (25)

    1043. Is It a Binary Search Tree (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    A Binary Search Tree (BST) is recursively defined as a binary tree which has 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 be 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 integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, first print in 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 is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

    Sample Input 1:
    7
    8 6 5 7 10 8 11
    
    Sample Output 1:
    YES
    5 7 6 8 11 10 8
    
    Sample Input 2:
    7
    8 10 11 8 6 7 5
    
    Sample Output 2:
    YES
    11 8 10 7 5 6 8
    
    Sample Input 3:
    7
    8 6 8 5 10 9 11
    
    Sample Output 3:
    NO
    

     

    检测是否为二叉搜索树或镜像二叉搜索树,先建树,再测试,镜像将left和right的位置互换即可。

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int N;
    int proOrder[1005];
    struct Node {
        Node* left;
        Node* right;
        int value;
    }* head, *temp;
    
    int INDEX = 0;
    int flag = 1, flag1 = 1;
    
    void proOrderFun(Node* node) {
        if(flag == 0) return;
        if(proOrder[INDEX] != node->value) {
            flag = 0;
            return;
        }
        INDEX++;
        if(node->left != NULL) {
            proOrderFun(node->left);
        }
        if(node->right != NULL) {
            proOrderFun(node->right);
        }
    }
    
    void proOrderFun1(Node* node) {
        if(flag1 == 0) return;
        if(proOrder[INDEX] != node->value) {
            flag1 = 0;
            return;
        }
        INDEX++;
        if(node->right != NULL) {
            proOrderFun1(node->right);
        }
        if(node->left != NULL) {
            proOrderFun1(node->left);
        }
    }
    
    int flag2 = 0;
    
    void postOrderFun(Node* node) {
        if(node->left != NULL) {
            postOrderFun(node->left);
        }
        if(node->right != NULL) {
            postOrderFun(node->right);
        }
        if(flag2 != 0) cout<< " ";
        flag2 = 1;
        cout<< node->value;
    }
    
    void postOrderFun1(Node* node) {
        if(node->right != NULL) {
            postOrderFun1(node->right);
        }
        if(node->left != NULL) {
            postOrderFun1(node->left);
        }
        if(flag2 != 0) cout<< " ";
        flag2 = 1;
        cout<< node->value;
    }
    
    int main()
    {
        cin>> N;
        for(int i = 0; i < N; i++) {
            cin>> proOrder[i];
            Node* node = new Node();
            node->value = proOrder[i];
            node->left = NULL;
            node->right = NULL;
            if(i == 0) {
                head = node;
            }
            else {
                temp = head;
                while(1) {
                    //cout<< temp->value<< " ";
                   if(node->value < temp->value) {
                        if(temp->left != NULL) {
                            temp = temp->left;
                        } else {
                            temp->left = node;
                            break;
                        }
                   }
                   else if(node->value >= temp->value) {
                        if(temp->right != NULL) {
                            temp = temp->right;
                        } else {
                            temp->right = node;
                            break;
                        }
                   }
                }
            }
        }
        proOrderFun(head);
        INDEX = 0;
        proOrderFun1(head);
        if(flag) {
            cout<< "YES"<< endl;
            postOrderFun(head);
        }
        else if(flag1) {
            cout<< "YES"<< endl;
            postOrderFun1(head);
        }
        else cout<< "NO"<< endl;
        return 0;
    }
  • 相关阅读:
    laravel路由和MVC
    laravel目录介绍
    laravel下载安装
    Mac 程序员的十种武器
    Python中列表的copy方法
    Ubuntu 安装vim失败解决
    Linux userAdd 增加用户如果没有配置文件情况解决
    Ubuntu 软件管理
    awk工具详解
    httpsClient
  • 原文地址:https://www.cnblogs.com/ACMessi/p/8435433.html
Copyright © 2011-2022 走看看