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 ...
随机推荐
- Shell编程基础知识(一)
一.基本的运行Linux程序的3种方法: (1) 使文件具有可执行权限,直接运行文件.eg: chmod a+x testfile.sh ./testfile.sh (2) 直接调用命令解释器来 ...
- Ubuntu 无法进行SSH连接,开启22端口
我们在VM中安装好Ubuntu 虚拟机后,经常需要使用Xshell等工具进行远程连接,但是会出现无法连接的问题,原因是Ubuntu中默认关闭了SSH 服务. 1. 查看Ubuntu虚拟机IP地址: 命 ...
- 我的BRF+自学教程(二):跟踪模式(trace mode)
使用自开发程序来处理业务逻辑时,处理过程通常是个黑箱,业务顾问和业务用户不知道程序的具体运行方式,要依赖文档和频繁的沟通来确认实际情况. BRFplus可以通过配置的方式实现业务逻辑,使得业务人员把业 ...
- UDP Health Checks
This chapter describes how to configure different types of health checks for UDP servers in a load-b ...
- CentOS TinyProxy http(s)上网代理及置代理上网的方法
http://blog.csdn.net/fwj380891124/article/details/42168683 http://computer.uoh.edu.cn/linux/2159.htm ...
- WPF设计の不规则窗体
我们在工作中,经常会需要画一些不规则的窗体,现在总结如下. 一.利用VisualBrush实现.这依赖于VisualBrush的特性,任何控件可以作为画刷,而画刷又可以作为背景. 此种方法可以用于实现 ...
- C++ 星号* 与 引用&
星号 * 1. 声明的时候有*, 表示指针变量 int *p=&a;// '&'的作用就是把a变量在内存中的地址给提取出来 2. * +地址, 表示地址操作符 3. 数字*数字, 表示 ...
- LightGBM介绍及参数调优
1.LightGBM简介 LightGBM是一个梯度Boosting框架,使用基于决策树的学习算法.它可以说是分布式的,高效的,有以下优势: 1)更快的训练效率 2)低内存使用 3)更高的准确率 4) ...
- ogg BR – BOUNDED RECOVERY 测试案例
首先,我们来看两个OGG同步中可能的问题: l oracle在线日志包含已提交的和未提交的事务,但OGG只会将已提交的事务写入到队列文件.因此,针对未提交的事务,特别是未提交的长事务,OGG会怎样处理 ...
- 域名打开没有加上“http://”,导致报错{"code":-32603,"message":"Cannot navigate to invalid URL"}
1.在robotframework中写用例 Open Browser 192.168.4.110:8880/jwzh Chrome 2.没有写http:// 3.导致报错 4.正确写法应该是 Op ...