1135 Is It A Red-Black Tree (30 point(s))

1135 Is It A Red-Black Tree (30分) 提供测试样例参考 题目大意:根据红黑树的判断规则,输入一个树的先序遍历,判断这个树是不是红黑树。 思路:无脑建树~ 1.由于是搜索树,中序遍历就是结点从小到大排序。 2.dfs搜索每一条根结点到叶子结点NULL的路径。(注意叶子结点是NULL!我一开始就没有理解题意,所以测试点2,3被卡了一下) 3.判断路径是不是符合题目给出的规则。 提供一个测试样例: 1 3 1 2 -3 No 应该题目给的样例+这个样例能通... 阅读详情

1135 Is It A Red-Black Tree (30 point(s))

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:

  • (1) Every node is either red or black.
  • (2) The root is black.
  • (3) Every leaf (NULL) is black.
  • (4) If a node is red, then both its children are black.
  • (5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.

rbf1.jpgrbf2.jpgrbf3.jpg
Figure 1Figure 2Figure 3

For each given binary search tree, you are supposed to tell if it is a legal red-black tree.

Input Specification:

Each input file contains several test cases. The first line gives a positive integer K (≤30) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.

Output Specification:

For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.

Sample Input:

3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17

Sample Output:

Yes
No
No

 红黑树的判断。

二叉搜索树的建立、计算从任意一个结点到叶子的黑色结点个数、动态结点二叉树

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
int K,N;bool isOK;
struct Node{
	int data;Node* left;Node* right;
	Node(int d):data(d),left(nullptr),right(nullptr){}
};
void insert(Node* &root,int key){
	if(root==nullptr){
		root = new Node(key);
		return;
	}
	if(abs(key)<abs(root->data)) insert(root->left,key);
	else insert(root->right,key);
}
set<int> s;
void getBlack(Node* root,int cnt){
	if(root==nullptr){
		s.insert(cnt);
		return;
	}
	if(root->data>0) cnt++;
	getBlack(root->left,cnt);
	getBlack(root->right,cnt);
}
void dfs(Node* root){
	if(root==nullptr) return;
	if(root->data<0){
		if(root->left!=nullptr&&root->left->data<0) {isOK = false;return;}
		if(root->right!=nullptr&&root->right->data<0) {isOK = false;return;}
	}
	s.clear();
	getBlack(root,0);
	if(s.size()!=1) {isOK = false;return;}
	if(root->left!=nullptr) dfs(root->left);
	if(root->right!=nullptr) dfs(root->right);
}
int main(void){
	cin>>K;int a;
	while(K--){
		cin>>N;
		Node* root = nullptr;
		isOK = true;
		for(int i=0;i<N;i++){
			cin>>a;
			insert(root,a);
		}
		if(root!=nullptr&&root->data<0) isOK=false;
		if(isOK){
			dfs(root);
		}
		if(isOK) puts("Yes");
		else puts("No");
	}
	return 0;
}

大神代码:

参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/82292391 

#include<bits/stdc++.h>
using namespace std;
struct Node{//二叉树结点定义
    int data,leftNum,rightNum;
    Node*left,*right;
    Node(int d):data(d),left(nullptr),right(nullptr),leftNum(0),rightNum(0){}
};
void insertTree(Node*&root,int data){//二叉查找树的插入节点算法
    if(root==nullptr){
        root=new Node(data);
        return;
    }
    if(abs(root->data)>abs(data))
        insertTree(root->left,data);
    else
        insertTree(root->right,data);
}
int getBlackNum(Node*root){//计算左右子树中黑色结点个数
    if(root==nullptr)
        return 1;
    root->leftNum=getBlackNum(root->left);//计算左子树中黑色结点个数
    root->rightNum=getBlackNum(root->right);//计算右子树中黑色结点个数
    return root->data<0?root->leftNum:root->leftNum+1;
}
bool judge(Node*root){//判断给定的树是否满足4、5条性质
    if(root==nullptr)
        return true;
    bool leftBlack=root->left==nullptr||root->left->data>0;
    bool rightBlack=root->right==nullptr||root->right->data>0;
    bool f1=root->data>0?true:leftBlack&&rightBlack;//是否满足性质4
    bool f2=root->leftNum==root->rightNum;//是否满足性质5
    return f1&&f2&&judge(root->left)&&judge(root->right);//递归判断左右子树
}
int main(){
    int K,N,a;
    scanf("%d",&K);
    while(K--){
        scanf("%d",&N);
        Node*root=nullptr;
        for(int i=0;i<N;++i){
            scanf("%d",&a);
            insertTree(root,a);
        }
        getBlackNum(root);
        bool isRBT=root->data>0&&judge(root);
        printf("%s\n",isRBT?"Yes":"No");
    }
    return 0;
}

 

图像质量评估:使用 SSIM 计算图像相似性 在图像处理领域,衡量两幅图像之间相似性的一种常见方法是使用结构相似性指数(SSIM)。SSIM 是一种全参考的图像质量评估指标,它不仅考虑了图像的亮度、对比度,还考虑了结构信息。在本文中,我们将介绍一个使用 Python 和 OpenCV 实现的 SSIM 计算脚本,并使用该脚本评估两幅图像的相似性。 阅读详情

相关推荐

HyperLPR3车牌识别-Android-SDK光速部署与使用

基于深度学习高性能中文车牌识别 High Performance Chinese License Plate Recognition Framework.快速部署AndroidSDK

tunm 6121

1135 Is It A Red-Black Tree (30point(s)) - C语言 PAT 甲级

1. 因为是二叉搜索树,且给了先序序列,所以可以直接建立一颗二叉搜索树 2. 判断题目所给红黑树需满足的条件,仅需判断 2、4、5 条件 1. 判断 2:根节点为黑 2. 判断 4:节点为红,孩子节点为黑 3. 判断 5:递归判断每个节点到叶子节点包含的黑节点个数

有三只土豆 787

STRING数据库的蛋白质相互作用(PPI)网络分析

在转录调控相关的文献中,我们经常能够看到这样的蛋白质相互作用网络(protein protein interaction network,PPI network)。具体而言,这些相关的文献中首先通过RNA-seq、表达谱芯片或者蛋白质组分析等,找到了在不同分组样本间一系列的差异表达基因或蛋白。

weixin_43029125的博客 9458

1135 Is It A Red-Black Tree (30 point(s))

题解 红黑树性质的判断。 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;vector&gt; #include&lt;algorithm&gt; using namespace std; vector&lt;int&gt; pre; int n, k; struct node { node *l, *r; int ...

徐伯莱 387

哨兵一号数据snap预处理_利用SNAP软件进行Sentinel-1A卫星微波影像的预处理

利用SNAP软件对雷达影像进行预处理,主要包括热噪声去除、轨道文件校正、辐射定标、滤波校正、多普勒地形校正,最终获得入射角和后向散射信息。数据:本次实验所用数据为哨兵一号(Sentinel-1A)干涉宽幅模式(Interferometric Wideswath,IW)下经过多视处理和地距转换的GRD格式产品一般处理流程如下:0 SNAP软件安装软件安装:安装成功后打开SNAP后如图所示:1 数据...

weixin_30000407的博客 1万+

量化交易新手必看:如何用vnpy+Tushare轻松获取A股历史数据(附完整代码)

本文为量化交易新手提供了使用vnpy和Tushare构建A股历史数据获取系统的完整指南。通过详细的代码示例,手把手教你搭建环境、封装数据获取引擎,并实现日线等数据的格式转换与本地存储,为策略回测与分析奠定坚实的数据基础。

weixin_29098117的博客 378

浙大 PAT 甲级 1135 Is It A Red-Black Tree 红黑树的判断

#include<stdio.h> #include<math.h> #include<Windows.h> #include<stdlib.h> typedef struct Node* Tree; struct Node { int key; Tree left; Tree right; bool red; };...

qq_39115541的博客 298

21、时间触发以太网:通信、诊断、故障隔离与参数化详解

本文详细解析了时间触发以太网(TTEthernet)的核心机制,包括团检测算法、诊断服务、故障隔离策略、配置服务以及协议参数化等内容。TTEthernet是一种专为超高可靠性系统设计的网络技术,广泛应用于航空航天、工业自动化和汽车电子等领域。文章还探讨了其未来发展趋势,如技术创新与生态建设,为开发者、企业和研究机构提供了应用与发展的方向。

efc12345678的博客 110

【1135】Is It A Red-Black Tree (30分)【红黑树】

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<algorithm> #include<map> #include<vector> #inclu...

发现问题,并解决问题,批判性思维 707

YOLOv12改进策略【Head】| ASFF 自适应空间特征融合模块,改进检测头Detect_ASFF

在目标检测中,处理多尺度物体是一个具有挑战性的问题。虽然特征金字塔或多级特征塔是解决多尺度问题的常用方法,但在单阶段检测器中,不同尺度特征之间的不一致性限制了其性能提升。为了解决这个问题,提出了自适应空间特征融合(ASFF)模块。

Limiiiing的博客 1173

【AI智能体】基于OpenCode 搭建Skills环境与项目实战开发详解

基于OpenCode 搭建Skills环境与项目实战开发详解

congge 8902

1135 Is It A Red-Black Tree (30 分)

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every...

海绵的博客 1271

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【PAT】A1135 Is It A Red-Black Tree (30point(s))

文章目录A1135 Is It A Red-Black Tree (30point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis Author: CHEN, Yue Organization: 浙江大学 Time Limit: 400 ms Memory Limit: 64 M...

Duke 282

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

1135 Is It A Red-Black Tree (30 point(s))

There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every...

张蔡辉 222

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

PAT A1135 Is It A Red-Black Tree (30point(s))

There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: · (1) Every node is either red or black. · (2) The root is black. · ...

qq_42347617的博客 260

PAT(A) 1135 Is It A Red-Black Tree (30 point(s))

性质:二叉搜索树的中序遍历序列为递增序列 前序+中序构建二叉搜索树 审题不够细致,(4)(5)性质都是for each node #include<bits/stdc++.h> using namespace std; struct node{ int left, right, value; }; int K, N; vector<node> v; vector<int> pre, in; set<int> black; bool cmp(i.

Anpotan的博客 109

PAT_甲级_1135 Is It A Red-Black Tree (30point(s)) (C++)【红黑树/先序+中序构建二叉树/判断红黑树性质】

目录 1,题目描述 题目大意 知识补充 2,思路 数据结构 算法 3,AC代码 4,解题过程 1,题目描述 Sample Input: 3 9 7 -2 1 5 -4 -11 8 14 -15 9 11 -2 1 -7 5 -4 8 14 -15 8 10 -7 5 -6 8 15 -11 17 Sample Output: Yes No No 题目大...

&再见萤火虫&的博客 346

PAT 1135 Is It A Red-Black Tree (30point(s))(做题记录)

Two days ago,I submitted the code but it wasn’t accepted.Then I spent a lot of time to change it and finally it ac! Note: the simple path in Property 5 means that path from each node to NULL! (that is the leaf!You can get this from property 3).In my previ

momoomoo的博客 351

1135 Is It A Red-Black Tree 1135 Is It A Red-Black Tree(30 分)

1135 Is It A Red-Black Tree(30 分) There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black....

花とアリス殺人事件 580

PAT甲级 1135. Is It A Red-Black Tree (30)

https://www.patest.cn/contests/pat-a-practise/1135 There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node

akibayashi的博客 2570

[C++] PAT 1135 Is It A Red-Black Tree (30分)

Sample Input: 3 9 7 -2 1 5 -4 -11 8 14 -15 9 11 -2 1 -7 5 -4 8 14 -15 8 10 -7 5 -6 8 15 -11 17 Sample Output: Yes No No 题解: 本题考察红黑树,可以根据题目给出的数据直接构建二叉树,不需要去了解红黑树在构建过程中的左旋右旋以及其他规则, 根据红黑树的定义: 1.每个节点只能是红色或黑色 2.根节点是黑色 3.每个为NULL的叶子节点是黑色 4.如果节点为红色,他的左右孩子均为黑色.

快乐de约定的博客 2021

PAT 甲级 1135  Is It A Red-Black Tree

1135 Is It A Red-Black Tree (30 point(s)) There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red o...

漫浸天空的雨色的备忘录~ 879

PAT 1135 Is It A Red-Black Tree

PAT 1135 Is It A Red-Black Tree There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3) Every leaf (NULL) is black. (4)

Del Piero的博客 238

『1135』Is It A Red-Black Tree

There is a kind of balanced binary search tree namedred-black treein the data structure. It has the following 5 properties: (1) Every node is either red or black. (2) The root is black. (3...

weixin_52435933的博客 255

1135. Is It A Red-Black Tree (30)

1135. Is It A Red-Black Tree (30)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueThere is a kind of balanced binary search tree named red-black tree in the data structure. It has the followi...

石前有座桥的博客 455

PAT(A) 1135. Is It A Red-Black Tree (30)

原题目: 原题链接:https://www.patest.cn/contests/pat-a-practise/1135 1135. Is It A Red-Black Tree (30) There is a kind of balanced binary search tree named red-black tree in the data structure...

萧易桥的博客 769
上一篇: 1143 Lowest Common Ancestor (30 point(s))
下一篇: 1133 Splitting A Linked List (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值