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 ...
随机推荐
- 洛谷P4012 深海机器人问题(费用流)
传送门 图给的好坑……还得倒过来…… 用大佬的图做个示范 我们考虑左图吧 把每一个点向下连边,容量$1$,费用为给出的价值(表示一个机器人可以过去取得标本) 再连一条边,容量$inf$,费用$0$(表 ...
- AR资料汇总学习
ARKit 从入门到精通 http://www.chinaar.com/ARKit/5210.html ARKit 框架的学习 http://blog.csdn.net/biangabiang/art ...
- Exalogic硬件架构
1.硬件配置见如下图表. 组件名称 满配 半配 1/4配 1/8配 Sun Rack II 1242 1 1 1 1 计算节点 X2-2.X3-2.X4-2.X5-2.X6-2 30 16 8 4 存 ...
- Javascript 中 的坑..
### 1. 0 == '' 返回 true 0 === '' 返回false......... 切记...
- Python- sort()/sorted()
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. sorted(iterable,key=None,revers ...
- python-使用字典使Fibonacci更有效率
原代码: def fib(n): if n == 1: return 1 elif n == 2: return 2 else: return fib(n-1)+fib(n-2) 改进后: def f ...
- 解决分批次调用 jsonp 接口的 callback 会报错问题
当我们分批次调用同一个jsonp接口时,会有一定机率同时调用,而jsonp的callback不支持同时调用, 会报错,所以当我们在分批次调用同一jsonp接口时,最好在callback后加个变量值,总 ...
- PhpExcel中文帮助手册|PhpExcel使用方法 ( 后面有部分没有显示出来 可以编辑中看到!!)
下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...
- python附录-builtins.py模块str类源码(含str官方文档链接)
python附录-builtins.py模块str类源码 str官方文档链接:https://docs.python.org/3/library/stdtypes.html#text-sequence ...
- python开头——文件声明 详解
一.解释器声明 1.声明方式 linux #!/usr/bin/python windowns #!c:/python27/python.exe 放在首行 2.作用 告诉电脑,要用/usr/bin下面 ...