二叉查找树——A1043.Is it a Binary Search Tree(25)

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct node{
    int data;
    node *left;
    node *right;
};
void insert(node* &root,int data){
    if(root == NULL){
        root = new node;
        root->data = data;
        root->left = NULL;
        root->right = NULL;
    }
    else if(data < root->data){
        insert(root->left,data);
    }else{
        insert(root->right,data);
    }
}
void preOrder(node* root,vector<int>& temppreorder){
    if(root == NULL){
        return;
    }
    temppreorder.push_back(root->data);
    preOrder(root->left,temppreorder);
    preOrder(root->right,temppreorder);
}
void preMirro(node* root,vector<int>& tempMirrorder){
    if(root == NULL){
        return;
    }
    tempMirrorder.push_back(root->data);
    preMirro(root->right,tempMirrorder);
    preMirro(root->left,tempMirrorder);
}
void postOrder(node* root,vector<int>& postorder){
    if(root == NULL){
        return;
    }
    postOrder(root->left,postorder);
    postOrder(root->right,postorder);
    postorder.push_back(root->data);
}
void postMirrOrder(node* root,vector<int>& postMirrorder){
    if(root == NULL){
        return;
    }
    postMirrOrder(root->right,postMirrorder);
    postMirrOrder(root->left,postMirrorder);
    postMirrorder.push_back(root->data);
}
int main(){
    int n;
    vector<int> initial,postorder,postMirrorder;
    vector<int> temppreorder;
    vector<int> tempMirrorder;
    scanf("%d",&n);
    node *root = NULL;
    int t;
    for(int i =0;i<n;++i){
        scanf("%d",&t);
        initial.push_back(t);
        insert(root,t);
    }
    preOrder(root,temppreorder);
    preMirro(root,tempMirrorder);
    bool order = true,mirrOrder = true;
    for(int i = 0;i<initial.size();++i){
        if(initial[i] != temppreorder[i]){
            order = false;
        }
        if(initial[i] != tempMirrorder[i]){
            mirrOrder = false;
        }
    }
    /*
    for(int i=0;i<initial.size();++i){
        if(i != initial.size()-1){
            printf("%d",initial[i]);
        }else{
            printf("%d\n",initial[i]);
        }
    }
    for(int i=0;i<temppreorder.size();++i){
        if(i != temppreorder.size()-1){
            printf("%d",temppreorder[i]);
        }else{
            printf("%d\n",temppreorder[i]);
        }
    }*/
    /*for(int i=0;i<tempMirrorder.size();++i){
        if(i != tempMirrorder.size()-1){
            printf("%d",tempMirrorder[i]);
        }else{
            printf("%d\n",tempMirrorder[i]);
        }
    }*/
    if(order == true){
        printf("YES\n");
        postOrder(root,postorder);
        for(int i = 0;i<postorder.size();++i){
            if(i != postorder.size()-1){
                printf("%d ",postorder[i]);
            }else{
                printf("%d",postorder[i]);
            }
        }
    }
    else if(mirrOrder == true){
        printf("YES\n");
        postMirrOrder(root,postMirrorder);
        for(int i = 0;i<postMirrorder.size();++i){
            if(i != postMirrorder.size()-1){
                printf("%d ",postMirrorder[i]);
            }else{
                printf("%d",postMirrorder[i]);
            }
        }
    }else{
        printf("NO\n");
    }
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/JasonPeng1/p/12240010.html

时间: 2024-11-05 16:09:22

二叉查找树——A1043.Is it a Binary Search Tree(25)的相关文章

A1043. Is It a Binary Search Tree (25)

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

A1043 Is It a Binary Search Tree (25分)

一.技术总结 这一题是二叉排序树的问题,题目主要是给出二叉排序树的先序遍历或者二叉排序树镜像的先序遍历或其他,如果是前两种输出YES,并且输出各自的后序遍历.后者直接输出NO 关键在于创建树,据我观察发现无论是镜像的先序遍历还是原来二叉排序树的先序遍历,可以直接根据二叉排序树的特点进行树的创建.用inset函数,创建出来都是二叉排序树,没有镜像与不镜像之分.到了这一步,只需处理如何将这个标准的二叉排序树,变成镜像的先序遍历,其实只要将左右子树递归的顺序换一下即可.详细参考代码. 然后就是将其进行

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

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

1043. Is It a Binary Search Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1043. Is It a Binary Search Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

pat1043. 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

04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

PAT 1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 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 nod

1043 Is It a Binary Search Tree (25 分)

1043 Is It a Binary Search Tree (25 分) 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 no

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 th