A1043 Is It a Binary Search Tree (25分)

一、技术总结

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

二、参考代码

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    node *lchild, *rchild;
};
void insert(node* &root, int data){
    if(root == NULL){
        root = new node;
        root->data = data;
        root->lchild = root->rchild = NULL;//这句不能够漏掉
        return;
    }
    if(data < root->data) insert(root->lchild, data);
    else insert(root->rchild, data);
}
//先序遍历结果存于vi
void preOrder(node* root, vector<int> &vi){
    if(root == NULL) return;
    vi.push_back(root->data);
    preOrder(root->lchild, vi);
    preOrder(root->rchild, vi);
}
//镜像树的先序遍历,结果存放于vi
void preOrderMirror(node* root, vector<int> &vi){
    if(root == NULL) return;
    vi.push_back(root->data);
    preOrderMirror(root->rchild, vi);
    preOrderMirror(root->lchild, vi);
}
//后序遍历结果存于vi
void postOrder(node* root, vector<int> &vi){
    if(root == NULL) return;
    postOrder(root->lchild, vi);
    postOrder(root->rchild, vi);
    vi.push_back(root->data);
}
//镜像树的后序遍历,结果存放于vi
void postOrderMirror(node* root, vector<int> &vi){
    if(root == NULL) return;
    postOrderMirror(root->rchild, vi);
    postOrderMirror(root->lchild, vi);
    vi.push_back(root->data);
}
vector<int> origin, pre, preM, post, postM;
int main(){
    int n, data;
    node* root = NULL;
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &data);
        origin.push_back(data);
        insert(root, data);
    }
    preOrder(root, pre);
    preOrderMirror(root, preM);
    postOrder(root, post);
    postOrderMirror(root, postM);
    if(origin == pre){
        printf("YES\n");
        for(int i = 0; i < n; i++){
            if(i != 0) printf(" ");
            printf("%d", post[i]);
        }
    }else if(origin == preM){
        printf("YES\n");
        for(int i = 0; i < n; i++){
            if(i != 0) printf(" ");
            printf("%d", postM[i]);
        }
    }else{
        printf("NO\n");
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/tsruixi/p/12333745.html

时间: 2024-10-29 14:28:33

A1043 Is It a Binary Search Tree (25分)的相关文章

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

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

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 node contains only nodes with keys greate

PAT Advanced 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 node contains only nodes with keys greate

7-20 Binary Search Tree (25分)

A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tree may correspond to several different insertion sequences. Now given several insertion sequences

二叉查找树——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

【PAT甲级】1043 Is It a Binary Search Tree (25 分)(判断是否为BST的先序遍历并输出后序遍历)

题意: 输入一个正整数N(<=1000),接下来输入N个点的序号.如果刚才输入的序列是一颗二叉搜索树或它的镜像(中心翻转180°)的先序遍历,那么输出YES并输出它的后序遍历,否则输出NO. trick: for(auto it:post) cout<<it<<((it!=post[n-1])?" ":""); 这样输出会使第0,1数据点格式错误...原因未知 cout<<post[0]; for(int i=1;i<

1043 Is It a Binary Search Tree (25分)

1. 题目 2. 思路 如下图 发现规律,对于最左边来说,后面所有的集合都是先小后大或者先大后小,如果小大交错那么不符合 使用1中规律确定是否为镜像,结合二叉排序树的特点,用递归建立树 输出树的后序遍历 3. 注意点 发现规律比较困难 树的题目一般都要用递归 4. 代码 #include<cstdio> #include<algorithm> #include<vector> using namespace std; #define null NULL struct n

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