当前位置:网站首页>二叉树是否对称
二叉树是否对称
2022-08-04 19:37:00 【-JMY-】
题目描述
假设一棵二叉树中每个结点的两棵子树都是空,或者都不为空,我们称该二叉树是对称二叉树。
给定一棵二叉树的字符串表示的结果,请编程判断,该二叉树是否是对称二叉树。
例如:下图所示的两棵二叉树中,T1是对称二叉树,T2不是对称二叉树。
二叉树用顺序结构给出,若读到#则该结点为空,二叉树T1=ABCDE,T2=ABCD#E,如果二叉树是对称的,输出“Yes”,反之输出“No”。

输入
一行字符串,表示二叉树的顺序存储序列,用字符#代表空结点,长度不大于1000。
输出
如果二叉树是对称的,输出“Yes”,反之输出“No”。
样例输入
ABCDE
样例输出
Yes 参考代码:
#include<bits/stdc++.h>
using namespace std;
int a[1005];
string s;
bool f(int x){
if(a[x*2]==0&&a[x*2+1]==0)
return true;
else if(a[x*2]!=0&&a[x*2+1]!=0)
return f(x*2) && f(x*2+1);
else
return false;
}
int main(){
cin>>s;
for(int i=0;i<s.size();i++)
if(s[i]!='#')
a[i+1]=1;
if(f(1))
cout<<"Yes";
else
cout<<"No";
return 0;
}
边栏推荐
猜你喜欢

Ant Group's time series database CeresDB is officially open source

Exploration and Practice of Database Governance

Pedestrian fall detection experiment based on YOLOV5

正畸MIA微种植体支抗技术中国10周年交流会在沈举办

SIGIR 2022 | 邻域建模Graph-Masked Transformer,显著提高CTR预测性能

正则表达式未完

指静脉识别-matlab

小软件大作用 | 如何省时省力进行Gerber图层快速对比?

Quantitative trading robot system development

Regular expression is incomplete
随机推荐
带你了解数据分布式存储原理
ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators
MySQL远程备份策略举例
JS: 数组和树的相互转换
小软件大作用 | 如何省时省力进行Gerber图层快速对比?
Notepad++更改显示背景
Client Side Cache 和 Server Side Cache 的区别
zynq records
[Sql刷题篇] 查询信息数据--Day1
华为WLAN技术:AP上线及相关模板的配置实验
奥拉时钟芯片生成配置文件脚本
SAP UI5 ensures that the control id is globally unique implementation method
【有奖征文】秋招特训,打造你的专属产品体验
T+Cloud: A "Smart Company" for Building New Business Social Networks and Marketing Relationships
How to promote the implementation of rural revitalization
Use "green computing" technology to promote sustainable development of computing power
【ASP.NET Core】 中间件
MogDB学习笔记-环境准备及单实例安装
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
Differences in the working mechanism between SAP E-commerce Cloud Accelerator and Spartacus UI