#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. [51nod1236] 序列求和 V3(斐波那契数列)

    题面 传送门 题解 把求和的柿子用斐波那契数列的通项公式展开 \[ \begin{aligned} Ans &=\sum\limits_{i = 1}^{n} \left(\frac{(\fr ...

  2. redis源码分析(3)sds

    sds是redis中用来处理字符串的数据结构.sds的定义在sds.h中: typedef char *sds; 简洁明了!简明扼要!(X,玩我呢是吧!这特么不就是c中的字符串么?!).像redis这 ...

  3. linux下 zip解压 tar解压 gz解压 bz2等各种解压文件命令

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...

  4. Web Service入门

    [IT168 技术文档] 一.什么是Web Service?     Web Service是构建互联网分布式系统的基本部件.Web Services 正成为企业应用集成(Enterprise App ...

  5. Python登陆人人网

    #!coding:utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): #登 ...

  6. 跟我一起读postgresql源码(一)——psql命令

    进公司以来做的都是postgresql相关的东西,每次都是测试.修改边边角角的东西,这样感觉只能留在表面,不能深入了解这个开源数据库的精髓,遂想着看看postgresql的源码,以加深对数据库的理解, ...

  7. 003 Android常见错误汇总

    1.installation failed with message invalid file 解决办法: <1>.点击工具栏上的Build中的Clean Project <2> ...

  8. C++_基础6-名称空间

    名称空间 背景:C++中,名称可以是变量.函数.结构.枚举.类以及类和结构的成员.当随着项目的增大,名称相互冲突的可能性也将增加.例如许多厂商的类库,可能导致名称冲突.这种冲突被称为名称空间问题. 解 ...

  9. C++_类和动态内存分配6-复习各种技术及队列模拟

    知识点: 队列:是一种抽象的数据类型(Abstract Data Type),可以存储有序的项目序列. 新项目被添加在队尾,并可以删除队首的项目.队列有些像栈.栈是在同一端进行添加和删除.这使得栈是一 ...

  10. docker image rm ubuntu 失败

    [root@hadoop14 ~]# docker image rm ubuntu Failed to remove image (ubuntu:v2): Error response from da ...