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


提交代码

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <iostream>
  4 #include <string.h>
  5 #include <math.h>
  6 #include <algorithm>
  7 #include <string>
  8 #include <stack>
  9 #include <queue>
 10 using namespace std;
 11 int n;
 12 struct tree{
 13     int data;
 14     tree *left;
 15     tree *right;
 16 };
 17
 18 void  insert(tree *&root,int data)
 19 {
 20     if(root==NULL)
 21     {
 22      root=new tree;
 23      root->data=data;
 24      root->left=NULL;
 25      root->right=NULL;
 26      return ;
 27     }
 28     if(data<root->data)insert(root->left,data);
 29     else insert(root->right,data);
 30 }
 31
 32 void preorder(tree * root,vector<int> &pre)
 33 {
 34     if(root==NULL)return;
 35     pre.push_back(root->data);
 36     preorder(root->left,pre);
 37     preorder(root->right,pre);
 38
 39 }
 40 void preorderM(tree * root,vector<int> &preM)
 41 {
 42     if(root==NULL)return ;
 43     preM.push_back(root->data);
 44     preorderM(root->right,preM);
 45     preorderM(root->left,preM);
 46
 47 }
 48 void postorder(tree * root,vector<int> &post)
 49 {
 50     if(root==NULL)return ;
 51     postorder(root->left,post);
 52     postorder(root->right,post);
 53     post.push_back(root->data);
 54 }
 55 void postorderM(tree * root,vector<int> &postM)
 56 {
 57     if(root==NULL)return;
 58     postorderM(root->right,postM);
 59     postorderM(root->left,postM);
 60     postM.push_back(root->data);
 61 }
 62
 63 vector<int> origin,pre,preM,post,postM;
 64 int main(){
 65      scanf("%d",&n);
 66      tree * root=NULL;
 67      for(int i=0;i<n;i++)
 68      {
 69          int temp;
 70          scanf("%d",&temp);
 71          origin.push_back(temp);
 72          insert(root,temp);
 73      }
 74
 75     //树已经插入好,开始先序遍历
 76      preorder(root,pre);
 77     //镜像先序遍历
 78      preorderM(root,preM) ;
 79
 80
 81      postorder(root,post);
 82      postorderM(root,postM);
 83      if(origin==pre)
 84      {
 85          printf("YES\n");
 86          for(int i=0;i<post.size();i++)
 87          {
 88              printf("%d",post[i]);
 89              if(i<post.size()-1)printf(" ");
 90          }
 91      }else if(origin==preM)
 92      {
 93          printf("YES\n");
 94          for(int i=0;i<postM.size();i++)
 95          {
 96              printf("%d",postM[i]);
 97              if(i<postM.size()-1)printf(" ");
 98          }
 99      }else
100      {
101          printf("NO\n");
102      }
103
104
105
106     return 0;
107 }
时间: 2024-08-05 15:20:41

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

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

A1043 Is It a Binary Search Tree (25分)

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

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

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

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分)(树的插入)

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