【PAT】【Advanced Level】1123. Is It a Complete AVL Tree (30)

2024-06-17 04:18

本文主要是介绍【PAT】【Advanced Level】1123. Is It a Complete AVL Tree (30),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1123. Is It a Complete AVL Tree (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

    

    

Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<= 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print "YES" if the tree is complete, or "NO" if not.

Sample Input 1:
5
88 70 61 63 65
Sample Output 1:
70 63 88 61 65
YES
Sample Input 2:
8
88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68
NO
原题链接:

https://www.patest.cn/contests/pat-a-practise/1123

思路:

首先平衡树

然后判断是否是满的平衡树

由于是平衡树

所以叶节点一定在最后两层

判断条件:

所有倒数第二层的叶节点在 中序遍历 序列中 一定在   层序遍历最后一个节点  的后边。

不能存在左子树为空的非叶节点

右子树为空的非叶节点一定是 层序遍历最后一个节点 的父亲

CODE:

#include<iostream>
#include<vector>
#include<algorithm>using namespace std;typedef struct S
{int ind;int val;int ls;int rs;int ld;int rd;int fl;int tra;
};
vector<S> T;
int trav;
vector<S> ro;
vector<S> le;void left(int n)
{S s1=T[n];S s2=T[T[n].rs];T[n].ind=n;T[n].val=s2.val;T[n].rs=s2.rs;T[n].ls=s2.ind;T[n].rd=s2.rd;T[n].ld=max(s1.ld,s2.ld)+1;T[s2.ind].ind=s2.ind;T[s2.ind].val=s1.val;T[s2.ind].rs=s2.ls;T[s2.ind].ls=s1.ls;T[s2.ind].rd=s2.ld;T[s2.ind].ld=s1.ld;}
void right(int n)
{S s1=T[n];S s2=T[T[n].ls];T[n].ind=n;T[n].val=s2.val;T[n].ls=s2.ls;T[n].rs=s2.ind;T[n].ld=s2.ld;T[n].rd=max(s1.rd,s2.rd)+1;T[s2.ind].ind=s2.ind;T[s2.ind].val=s1.val;T[s2.ind].ls=s2.rs;T[s2.ind].rs=s1.rs;T[s2.ind].ld=s2.rd;T[s2.ind].rd=s1.rd;}
int insert(int n,int v)
{if (v<T[n].val){if (T[n].ls==-1){S s;s.ind=T.size();s.val=v;s.ls=s.rs=-1;s.ld=s.rd=0;T[n].ls=T.size();T[n].ld=1;T.push_back(s);}else{int fl=insert(T[n].ls,v)+1;T[n].ld=fl;}}else{if (T[n].rs==-1){S s;s.ind=T.size();s.val=v;s.ls=s.rs=-1;s.ld=s.rd=0;T[n].rs=T.size();T[n].rd=1;T.push_back(s);}else{int fl=insert(T[n].rs,v)+1;T[n].rd=fl;}	}if (T[n].ld-T[n].rd>1){if (T[T[n].ls].rd>T[T[n].ls].ld){left(T[n].ls);}right(n);}else if (T[n].rd-T[n].ld>1){//cout<<v<<" "<<T[n].val<<" "<<T[n].ld<<" "<<T[n].rd<<endl;if (T[T[n].rs].ld>T[T[n].rs].rd){right(T[n].rs);}left(n);}//cout<<v<<" "<<T[n].val<<" "<<T[n].ld<<" "<<T[n].rd<<endl;return max(T[n].ld,T[n].rd);
}
void dfs(int n,int flo)
{T[n].fl=flo;if (T[n].ls!=-1)	dfs(T[n].ls,flo+1);T[n].tra=trav;trav++;if (T[n].rs!=-1)	dfs(T[n].rs,flo+1);
}bool cmp(S a, S b)
{if (a.fl==b.fl){return a.tra<b.tra;}else{return a.fl<b.fl;}	
}
int main()
{int n;cin>>n;S s;s.ind=0;s.ls=s.rs=-1;s.ld=s.rd=0;cin>>s.val;T.push_back(s);for (int i=1;i<n;i++){int t;cin>>t;insert(0,t);}	trav=0;dfs(0,0);sort(T.begin(),T.end(),cmp);bool flag=0;for (int i=0;i<n;i++){if (i!=0) cout<<" ";cout<<T[i].val; if ( (T[i].ls==-1&&T[i].rs==-1) && (T[i].fl==T[n-1].fl-1) && (T[i].tra<T[n-1].tra)){flag=1;}if ( (T[i].ls==-1&&T[i].rs!=-1)){flag=1;}if ( (T[i].ls!=-1&&T[i].rs==-1) && (T[i].ls!=T[n-1].ind) ){flag=1;}}cout<<endl;if (flag==0)	cout<<"YES";else	cout<<"NO";return 0;
}





这篇关于【PAT】【Advanced Level】1123. Is It a Complete AVL Tree (30)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068456

相关文章

30常用 Maven 命令

Maven 是一个强大的项目管理和构建工具,它广泛用于 Java 项目的依赖管理、构建流程和插件集成。Maven 的命令行工具提供了大量的命令来帮助开发人员管理项目的生命周期、依赖和插件。以下是 常用 Maven 命令的使用场景及其详细解释。 1. mvn clean 使用场景:清理项目的生成目录,通常用于删除项目中自动生成的文件(如 target/ 目录)。共性规律:清理操作

2024网安周今日开幕,亚信安全亮相30城

2024年国家网络安全宣传周今天在广州拉开帷幕。今年网安周继续以“网络安全为人民,网络安全靠人民”为主题。2024年国家网络安全宣传周涵盖了1场开幕式、1场高峰论坛、5个重要活动、15场分论坛/座谈会/闭门会、6个主题日活动和网络安全“六进”活动。亚信安全出席2024年国家网络安全宣传周开幕式和主论坛,并将通过线下宣讲、创意科普、成果展示等多种形式,让广大民众看得懂、记得住安全知识,同时还

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

c++习题30-求10000以内N的阶乘

目录 一,题目  二,思路 三,代码    一,题目  描述 求10000以内n的阶乘。 输入描述 只有一行输入,整数n(0≤n≤10000)。 输出描述 一行,即n!的值。 用例输入 1  4 用例输出 1  24   二,思路 n    n!           0    1 1    1*1=1 2    1*2=2 3    2*3=6 4

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

嵌入式面试经典30问:二

1. 嵌入式系统中,如何选择合适的微控制器或微处理器? 在嵌入式系统中选择合适的微控制器(MCU)或微处理器(MPU)时,需要考虑多个因素以确保所选组件能够满足项目的具体需求。以下是一些关键步骤和考虑因素: 1.1 确定项目需求 性能要求:根据项目的复杂度、处理速度和数据吞吐量等要求,确定所需的处理器性能。功耗:评估系统的功耗需求,选择低功耗的MCU或MPU以延长电池寿命或减少能源消耗。成本

MiniCPM-V: A GPT-4V Level MLLM on Your Phone

MiniCPM-V: A GPT-4V Level MLLM on Your Phone 研究背景和动机 现有的MLLM通常需要大量的参数和计算资源,限制了其在实际应用中的范围。大部分MLLM需要部署在高性能云服务器上,这种高成本和高能耗的特点,阻碍了其在移动设备、离线和隐私保护场景中的应用。 文章主要贡献: 提出了MiniCPM-V系列模型,能在移动端设备上部署的MLLM。 性能优越:

【全网最全】2024年数学建模国赛A题30页完整建模文档+17页成品论文+保奖matla代码+可视化图表等(后续会更新)

您的点赞收藏是我继续更新的最大动力! 一定要点击如下的卡片,那是获取资料的入口! 【全网最全】2024年数学建模国赛A题30页完整建模文档+17页成品论文+保奖matla代码+可视化图表等(后续会更新)「首先来看看目前已有的资料,还会不断更新哦~一次购买,后续不会再被收费哦,保证是全网最全资源,随着后续内容更新,价格会上涨,越早购买,价格越低,让大家再也不需要到处买断片资料啦~💰💸👋」�

JobScheduler 调用导致的运行时长30分钟的功耗问题

一、SDK 的使用情况与功耗影响 案例是否导致功耗变大onStartJob return true 且子线程没有调用jobFinished()告知系统功耗变大,最长带来30分钟的partial wakelock 长持锁onStartJob return true 且子线程调用jobFinished()告知系统功耗有影响,主要线程执行时长,标准是30秒内onStartJob return fals

嵌入式面试经典30问:一

什么是嵌入式系统? 嵌入式系统是指嵌入到某个对象体系中的专用计算机系统,它负责执行特定的任务,具有专用性、隐蔽性、资源受限和可靠性要求高等特点。通常包括硬件和软件两部分,硬件以微处理器为核心,软件则负责控制和管理硬件资源,实现特定的应用功能。 嵌入式系统和普通计算机系统有什么区别? 嵌入式系统与普通计算机系统的主要区别在于目的、资源、性能和成本等方面。嵌入式系统通常针对特定应用设计,具有体积小