PAT L3-010 是否完全二叉搜索树
https://pintia.cn/problem-sets/994805046380707840/problems/994805049870368768
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。
输入格式:
输入第一行给出一个不超过20的正整数N;第二行给出N个互不相同的正整数,其间以空格分隔。
输出格式:
将输入的N个正整数顺序插入一个初始为空的二叉搜索树。在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格。第二行输出YES,如果该树是完全二叉树;否则输出NO。
输入样例1:
9
38 45 42 24 58 30 67 12 51
输出样例1:
38 45 24 58 42 30 12 67 51
YES
输入样例2:
8
38 24 12 45 58 67 42 51
输出样例2:
38 45 24 58 42 12 67 51
NO
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int a[maxn];
vector<int> v(maxn);
vector<int> ans[maxn];
int depth = -1, cnt = -1; struct Node{
int val;
struct Node *left, *right;
}; int Pow(int a, int b) {
int ans = 1;
if(b == 0) return 1;
for(int i = 1; i <= b; i ++)
ans *= a; return ans;
} Node *BuildBST(Node *root, int x) {
if(!root) {
root = new Node();
root -> val = x;
root -> left = NULL;
root -> right = NULL;
} else if(x <= root -> val)
root -> right = BuildBST(root -> right, x);
else root -> left = BuildBST(root -> left, x); return root;
} void dfs(Node* root, int step, int index) {
if(!root) {
depth = max(depth, step + 1);
return;
} v[step] ++;
ans[step].push_back(root -> val);
dfs(root -> left, step + 1, index * 2);
dfs(root -> right, step + 1, index * 2 + 1); cnt = max(cnt, index);
} int height(Node* root) {
if(!root) return 0;
return max(height(root -> left), height(root -> right)) + 1;
} int main() {
scanf("%d", &N);
Node *root = NULL;
for(int i = 0; i < N; i ++) {
scanf("%d", &a[i]);
root = BuildBST(root, a[i]);
}
dfs(root, 0, 1);
bool flag = true;
for(int i = 0; i < depth - 2; i ++) {
if(v[i] != Pow(2, i)) {
flag = false;
break;
}
} for(int i = 0; i < depth; i ++) {
for(int j = 0; j < ans[i].size(); j ++) {
if(i == 0 && j == 0) printf("");
else printf(" ");
printf("%d", ans[i][j]);
}
}
printf("\n");
if(height(root -> left) - height(root -> right) > 1) flag = false;
if(cnt == N) printf("YES");
else printf("NO");
return 0;
}
还有一种建树一会写吧
PAT L3-010 是否完全二叉搜索树的更多相关文章
- (PAT)L2-004 这是二叉搜索树吗?(数据结构)
题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...
- PAT 天梯赛 是否完全二叉搜索树 (30分)(二叉搜索树 数组)
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...
- PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...
- PAT (天梯)L2-004. 这是二叉搜索树吗?
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- PAT 天梯赛 L2-004 这是二叉搜索树吗?
递归判断+建树 题目链接:https://www.patest.cn/contests/gplt/L2-004 题解 二叉搜索树的特点就是其根节点的值是位于左右子树之间的,即大于左子树的所有值,但是小 ...
- PAT L3-016 二叉搜索树的结构
https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...
- PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
随机推荐
- java实现支付宝支付及退款(二)
紧跟上篇博客,本篇将书写具体的代码实现 开发环境:SSM.maven.JDK8.0 1.Maven坐标 <!--阿里支付--> <dependency> <groupId ...
- Photoshop怎么破解?PS怎么破解?
Photoshop和PS这两个软件可以说是十分常见的图片处理软件了,Photoshop主要处理以像素所构成的数字图像进行图片编辑工作,而PS就更加强大了,它有很多功能,在图像.图形.文字.视频.出版等 ...
- python 基本运算符
一.格式化输出 简易名片的制作 name="小龙女" phone=15464623646 firm="神雕侠侣" pro="神仙姐姐" pr ...
- Zookeeper Health Checks
Short Description: The article talks about the basic health checks to be performed when working on i ...
- 设计模式のDecoratorPattern(装饰器模式)----结构模式
一.产生背景 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...
- swift Class的内存布局
class Human { //8 type or isa //retainCount var age: Int?//16 var name: String?//16 var nicknames: [ ...
- centos7下升级SSH
Linux 发行版中集成的 SSH 软件版本都比较老,存在着一些漏洞和安全隐患,需要升级 SSH 服务软件修补漏洞提升系统安全. SSH:SSH 有许多标准,通常 Linux 中 ( Redhat, ...
- Python:Day05 格式化输出、列表
注释:3个单引号或3个双引号 3个引号(单引或双引)还有另外一个作用:打印多行. msg = """hello 1 hello 2 hello 3"" ...
- 转载:遇到BITMAP CONVERSION TO ROWIDS 后解决与思考
今天遇到一个案例,有点价值写下来,以后多看看 SQL: select t.order_id, t.spec_name, t.staff_code, t.staff_code as xxbStaffCo ...
- CF341D Iahub and Xors
CF341D Iahub and Xors 给定一个 \(n\times n\) 的矩阵,平面异或,求平面异或和 \((n\leq10^3,\ m\leq10^5)\) 树状数组 这里主要是记录一下板 ...