PAT 1043 Is It a Binary Search Tree
#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的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 甲级 1043 Is It a Binary Search Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805440976633856 A Binary Search Tree ( ...
- 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 ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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 ...
随机推荐
- 51nod1258 序列求和 V4(伯努利数+多项式求逆)
题面 传送门 题解 不知道伯努利数是什么的可以先去看看这篇文章 多项式求逆预处理伯努利数就行 因为这里模数感人,所以得用\(MTT\) //minamoto #include<bits/stdc ...
- django中ModelForm解决多表单组合显示问题
一.多表单组合显示问题 在项目中用ModelForm生成页面时 当有多表单组合显示时,会显示全部的关联表单数据. 而在实际项目中可能会出现只想让用户选择部分数据,这时候这样的显示就有问题. 二.问题解 ...
- TypeScript入门-类
▓▓▓▓▓▓ 大致介绍 在ECMASript6中引入了类这一概念,通过class声明一个类.对于学习过C和C++的人应该不会陌生 ▓▓▓▓▓▓ 类 看一个简单的类: class Greeter { g ...
- CF914E Palindromes in a Tree
$ \color{#0066ff}{ 题目描述 }$ 给你一颗 n 个顶点的树(连通无环图).顶点从 1 到 n 编号,并且每个顶点对应一个在'a'到't'的字母. 树上的一条路径是回文是指至少有一个 ...
- Optimizing and caching browser bundles...
扎心了,老铁!!! 他妈的,弄了几天都是卡在这里!!上网查了一下.说是我的内存不够,好吧.我慢慢等,然而让我等了好几天都没什么反应..两天了,一直卡在这里.今天在虚拟机也是发狠了. 我直接把虚拟机的内 ...
- C - 思考使用差分简化区间操作
FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positi ...
- opencv基本操作
src.convertTo(dst, type, scale, shift) 缩放并转换到另外一种数据类型: dst:目的矩阵 type:需要的输出矩阵类型,或者更明确的,是输出矩阵的深度,如果是负值 ...
- POJ - 2248 迭代加深
枚举ak的值 反向枚举使ak尽快到达最短链的n /*H E A D*/ int n,m,a[23333],dep; bool dfs(int x){ if(a[x-1]>n||a[x-1]< ...
- 今日工作总结:jquery轮转效果的集成与前台页面banner的设计思路总结
今日做了两个项目中的两个问题,现在特来总结一下,以便分享给更多的朋友们. 1.jquery轮转效果的集成 涉及到jquery的不同版本问题,解决办法是在后缀用jQuery代替$.项目地址在:121.4 ...
- ubuntu root用图形界面登录
http://blog.csdn.net/weimine/article/details/69055536 http://blog.csdn.net/gongxifacai_believe/artic ...