#include <cstdio>
#include <climits>
#include <cstdlib>
#include <vector> const double LO_BOUND = -1.0 + INT_MIN;
const double HI_BOUND = +1.0 + INT_MAX; using namespace std; class Node {
public:
Node* L;
Node* R;
int data;
Node(int d=, Node* l = NULL, Node* r = NULL) : L(l), R(r), data(d){}
}; Node* build_bst(vector<Node> &nums, int start, int end, bool mirror = false) {
if (start >= end) return NULL;
if (start + == end) {
nums[start].R = nums[start].L = NULL;
return &nums[start];
}
Node& root = nums[start];
int i = start + ;
while (i<end) {
if (!mirror) {
if (nums[i].data >= root.data) break;
} else {
if (nums[i].data < root.data) break;
}
i++;
}
// printf("Root: %d\n", start);
// printf("L: %d-%d\n", start + 1, i);
// printf("R: %d-%d\n", i, end); root.L = build_bst(nums, start + , i, mirror);
root.R = build_bst(nums, i, end, mirror); return &root;
} bool is_bst(Node* root, bool mirror = false, double lo = LO_BOUND, double hi = HI_BOUND) {
if (root == NULL) return true;
// printf("check %d lo=%f hi=%f mirror=%d\n", root->data, lo, hi, mirror);
if (root->data < lo || root->data >= hi) {
// printf("fail\n");
return false;
}
Node *vL = root->L, *vR = root->R, *tmp = NULL;
if (mirror) {
tmp = vL;
vL = vR;
vR = tmp;
}
return is_bst(vL, mirror, lo, root->data) && is_bst(vR, mirror, root->data, hi);
} void postorder(Node* root, vector<int> &v) {
if (root == NULL) return;
postorder(root->L, v);
postorder(root->R, v);
v.push_back(root->data);
return;
} void print(vector<int> &v) {
int len = v.size(); if (len < ) return;
printf("%d", v[]);
for (int i=; i<len; i++) {
printf(" %d", v[i]);
}
printf("\n");
}
int main() {
int N;
scanf("%d", &N);
vector<Node> nums(N); for (int i=; i<N; i++) {
scanf("%d", &(nums[i].data));
}
vector<int> post; Node* root = build_bst(nums, , nums.size());
if (is_bst(root)) {
printf("YES\n");
postorder(root, post);
print(post);
return ;
} root = build_bst(nums, , nums.size(), true);
if (is_bst(root, true)) {
printf("YES\n");
postorder(root, post);
print(post);
return ;
} else {
printf("NO\n");
}
return ;
}

就这样吧,还有那么多题拙计啊

PAT 1043 Is It a Binary Search Tree的更多相关文章

  1. PAT 1043 Is It a Binary Search Tree[二叉树][难]

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  2. PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  3. 【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 ...

  4. 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 bina ...

  5. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  6. PAT 甲级 1043 Is It a Binary Search Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...

  7. PAT Advanced 1043 Is It a Binary Search Tree (25) [⼆叉查找树BST]

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  8. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  9. 1043 Is It a Binary Search Tree (25 分)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. python3入门之字典

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 本节主要介绍字典,字典也成映射,时python中唯一内建的映射类型.更多详细请点击readmore.下面附有之前的文章: python入门之字符串 ...

  2. linux负载均衡与性能监控

    平均负载是单位时间内, 系统处于可运行状态和不可中断状态的平均进程数( 平均活跃进程数 ) 可运行状体好理解, 就是进程正在运行状态Running和可运行状态Runnable... 这里需要注意的是 ...

  3. iOS如何检测app从后台调回前台

    当按Home键,将应用放在后台之后,然后再次调用回来的时候,回出发AppDelegate里面的一个方法,-(void)applicationWillEnterForeground. 当应用再次回到后台 ...

  4. 29.最小的K个数

    题目描述:   输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路分析:   利用快速排序的partition函数,par ...

  5. mysql 彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1

    彻底解决:Incorrect string value: '\xF0\x9F\x98\xAD",...' for column 'commentContent' at row 1 今天在爬取 ...

  6. α测试,Beta测试

    α测试(内测)是由一个用户在开发环境下进行的测试,也可以是公司内部的用户在模拟实际操作环境下进行的测试.α测试的目的是评价软件产品的FLURPS(即功能.局域化.可使用性.可靠性.性能和支持).尤其注 ...

  7. c# 动态生成继承类并实现序列化特性

    项目来源 App传过来的字段是动态的,希望能保证扩展性,返回时,把所需要的字段与数据融合后再返回过去 数据是第3方来源的,但是序列化后的结果又希望能并列返回 如:App传过来 一个设备Id,客户Id等 ...

  8. [转] CSS3垂直手风琴折叠菜单

    [From] http://www.html5tricks.com/css3-ver-accordion-menu.html 之前我们已经分享过很多关于手风琴菜单了,有水平方向的,也有垂直方向的.今天 ...

  9. Tesorflow-自动编码器(AutoEncoder)

    直接附上代码: import numpy as np import sklearn.preprocessing as prep import tensorflow as tf from tensorf ...

  10. contentType 应用,(表中数据大量存在外键时使用)

    当表中有大量的foreignkey 外键时,而且后期还有可能加入一些外键时, 使用django自带的contenttype表(表结构是app名和表的对应的每个类字段) 1. Django 中的 con ...