1043. Is It a Binary Search Tree (25) -- 二搜索叉树构建 和 递归遍历
来源:互联网 发布:淘宝 自助服务 编辑:程序博客网 时间:2024/06/18 07:32
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 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:78 6 5 7 10 8 11Sample Output 1:
YES5 7 6 8 11 10 8Sample Input 2:
78 10 11 8 6 7 5Sample Output 2:
YES11 8 10 7 5 6 8Sample Input 3:
78 6 8 5 10 9 11Sample Output 3:
NO
http://www.patest.cn/contests/pat-a-practise/1043
#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <string>#include <vector>#include <set>#include <map>#include <iterator>using namespace std ;#define INF 999999#define N 1005int n; int a[N] ;typedef struct node{int data ;struct node* left ;struct node* right ;node(int _data){data = _data ;left = NULL ;right = NULL ;}}Bnode ;Bnode* createTree(Bnode* root , int data){if(root == NULL){root = new node(data) ;}else{if(data < root->data ){root->left = createTree(root->left , data );}else{root->right = createTree(root->right , data );}}return root ;}Bnode* createTree2(Bnode* root , int data){if(root == NULL){root = new node(data) ;}else{if(data >= root->data ){root->left = createTree2(root->left , data );}else{root->right = createTree2(root->right , data );}}return root ;}vector<int> pre ;void preOrder(Bnode* root){if(root != NULL){pre.push_back(root->data) ;preOrder(root->left) ;preOrder(root->right) ;}}vector<int> post ;void postOrder(Bnode* root){if(root != NULL){postOrder(root->left) ;postOrder(root->right) ;post.push_back(root->data) ;}}bool isYes(){int i ;for(i = 0 ; i < n ; i++){if(a[i] != pre[i])return false ;}return true ;}int main(){//freopen("in.txt","r",stdin);while(scanf("%d",&n) != EOF){Bnode* root = NULL ;int i ;for(i = 0 ; i < n; i ++){scanf("%d" , &a[i]) ;root = createTree(root , a[i] ) ;}pre.clear() ;preOrder(root) ;if(isYes()){printf("YES\n") ;post.clear() ;postOrder(root) ;printf("%d",post[0]);for(i = 1 ; i < n ; i++){printf(" %d",post[i]);}printf("\n") ;}else{root = NULL ;for(i = 0 ; i < n; i ++){root = createTree2(root , a[i] ) ;}pre.clear() ;preOrder(root) ;if(isYes()){printf("YES\n") ;post.clear() ;postOrder(root) ;printf("%d",post[0]);for(i = 1 ; i < n ; i++){printf(" %d",post[i]);}printf("\n") ;}else{printf("NO\n");}}}return 0 ;}
- 1043. Is It a Binary Search Tree (25) -- 二搜索叉树构建 和 递归遍历
- PAT 1043. Is It a Binary Search Tree (25)(判断是否是搜索树并构建和后序输出)(待修改)
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043.Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- 1043. Is It a Binary Search Tree
- PAT A 1043. Is It a Binary Search Tree (25)
- PAT-A-1043. Is It a Binary Search Tree (25)
- pat-a 1043. Is It a Binary Search Tree (25)
- Is It a Binary Search Tree (25)
- 【codevs 1230】元素查找 splay……
- Background类提取前景物体然后保存为图片
- android Uri获取真实路径转换成File的方法
- iOS GCD多线程使用
- viewDidLoad, viewWillDisappear, viewWillAppear等区别及各自的加载顺序
- 1043. Is It a Binary Search Tree (25) -- 二搜索叉树构建 和 递归遍历
- build.xml的语法格式
- WinAppDeployCmd部署Win10 App 到移动设备
- string与 Tstringlist的使用方式。
- 访问子字符串
- Angularjs自定义表单验证
- 定制以及彩色化自己的 Mac OS X 终端(2015.11.2更新)
- BLE介绍
- RecyclerView原理以及使用