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 是否完全二叉搜索树的更多相关文章

  1. (PAT)L2-004 这是二叉搜索树吗?(数据结构)

    题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...

  2. PAT 天梯赛 是否完全二叉搜索树   (30分)(二叉搜索树 数组)

    将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...

  3. PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. PAT (天梯)L2-004. 这是二叉搜索树吗?

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...

  6. PAT 天梯赛 L2-004 这是二叉搜索树吗?

    递归判断+建树 题目链接:https://www.patest.cn/contests/gplt/L2-004 题解 二叉搜索树的特点就是其根节点的值是位于左右子树之间的,即大于左子树的所有值,但是小 ...

  7. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Vue 学习笔记之快速入门篇

    Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还便于与 ...

  2. LeetCode算法题-Intersection of Two Arrays II(Java实现)

    这是悦乐书的第208次更新,第220篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第76题(顺位题号是350).给定两个数组,编写一个函数来计算它们的交集.例如: 输入: ...

  3. 使用Razor Generator构建模块化ASP.NET MVC应用程序

    在构建Web应用程序的时候,我们很难做到模块化的开发,这是因为Web应用程序不仅仅包含编译的C#代码,还包含了js.css和aspx等资源. 在ASP.NET MVC中,我们发布应用程序的时候,还会包 ...

  4. 英语进阶系列-A01-再别康桥

    每天必读知识 音标 发音网页 人称代词与物主代词 时态 朗读50遍词汇系列1 Number word 1 be 2 have 3 get 4 give 5 take 诗歌欣赏 [原诗] [英文版] 再 ...

  5. nuxt拦截IE浏览器

    需求场景 判断浏览器类型,让譬如IE的低版本浏览器跳转到指定提示浏览器升级页面. 难点分析 使用过的都知道,nuxt没有暴露主入口页面也就是index.html啊,我们以前常用的IE条件判断没地方写. ...

  6. JS页面打印

    平常浏览网页和文档的时候,随处可见打印两个字,有时候不小心点到或者快捷键触发到,就会弹出一个打印的页面,上边显示的打印机是GoldGrid Virtual Printer,这是计算机的虚拟打印机,打印 ...

  7. Unexpected end of JSON input while parsing near

    运行 npm cache clean --force 即可解决pm install出现”Unexpected end of JSON input while parsing near”错误.

  8. mybatis-plus学习笔记(一)

    一.概述 概述见官网,不再赘述(简称mytatis-plus为MP) 在mybatis的基础之上,重点关注新特性:https://mp.baomidou.com/guide/#%E7%89%B9%E6 ...

  9. Luogu P3165 [CQOI2014]排序机械臂

    先讲一下和这题一起四倍经验的题: Luogu P4402 [Cerc2007]robotic sort 机械排序 SP2059 CERC07S - Robotic Sort UVA1402 Robot ...

  10. C#使用Json.Net遍历Json

    StringBuilder builder=new StringBuilder(); builder.AppendLine("{"); builder.AppendLine(&qu ...