当前位置:网站首页>对称的二叉树【树的遍历】
对称的二叉树【树的遍历】
2022-07-07 01:25:00 【MC快乐的苦Xiao怕】
题
思路:dfs遍历n个节点,判断是否对称:
#include <bits/stdc++.h>
using namespace std ;
const int N = 10000010 ;
int l[N] , r[N] , a[N] ;
int m , ans = 1 ;
bool flag ;
inline int dfs (int x , int y , int s)
{
if (x == -1 && y == -1) return 0 ;
if (x == -1 || y == -1 && x != y)
{
flag = true ;
return 0 ;
}
if (a[x] != a[y])
{
flag = true ;
return 0 ;
}
return dfs (l[x] , r[y] , 2) + dfs (r[x] , l[y] , 2) + s ;
}
int main ()
{
scanf ("%d" , & m) ;
for (int i = 1; i <= m; i++) scanf ("%d" , & a[i]) ;
for (int i = 1; i <= m; i++) scanf ("%d%d" , & l[i] , & r[i]) ;
for (int i = 1; i <= m; i++)
{
int s = dfs (l[i] , r[i] , 3) ;
if (s > ans && flag == false) ans = s ;
flag = false ;
}
printf ("%d" , ans) ;
return 0 ;
}
边栏推荐
- 你不知道的互联网公司招聘黑话大全
- 目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
- 693. 行程排序
- Cf:c. column swapping [sort + simulate]
- mac版php装xdebug环境(m1版)
- PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
- Red hat install kernel header file
- JVM monitoring and diagnostic tools - command line
- A very good JVM interview question article (74 questions and answers)
- 980. 不同路径 III DFS
猜你喜欢
当我们谈论不可变基础设施时,我们在谈论什么
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core
外设驱动库开发笔记43:GPIO模拟SPI驱动
win系统下安装redis以及windows扩展方法
绕过open_basedir
Deep clustering: joint optimization of depth representation learning and clustering
搞懂fastjson 对泛型的反序列化原理
How to improve website weight
从“跑分神器”到数据平台,鲁大师开启演进之路
Jstat pour la commande JVM: voir les statistiques JVM
随机推荐
You don't know the complete collection of recruitment slang of Internet companies
Understand the deserialization principle of fastjson for generics
Swagger3 configuration
JVM命令之 jstat:查看JVM统计信息
PTA 天梯赛练习题集 L2-002 链表去重
PTA ladder game exercise set l2-002 linked list de duplication
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
[FPGA tutorial case 14] design and implementation of FIR filter based on vivado core
What EDA companies are there in China?
Career experience feedback to novice programmers
ML's shap: Based on the adult census income binary prediction data set (whether the predicted annual income exceeds 50K), use the shap decision diagram combined with the lightgbm model to realize the
MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
Vscode for code completion
Bypass open_ basedir
生活中的开销,怎么记账合适
Markdown 并排显示图片
STM32 key state machine 2 - state simplification and long press function addition
[FPGA tutorial case 13] design and implementation of CIC filter based on vivado core
JVM monitoring and diagnostic tools - command line
go-microservice-simple(2) go-Probuffer