当前位置:网站首页>对称的二叉树【树的遍历】
对称的二叉树【树的遍历】
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 ;
}
边栏推荐
- The boss always asks me about my progress. Don't you trust me? (what do you think)
- 生活中的开销,怎么记账合适
- 10W word segmentation searches per second, the product manager raised another demand!!! (Collection)
- 360织语发布7.0新品 为党政军、央国企打造专属“统一数字工作空间”
- Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
- Check point: the core element for enterprises to deploy zero trust network (ztna)
- If you don't know these four caching modes, dare you say you understand caching?
- Jmeter自带函数不够用?不如自己动手开发一个
- [solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
- From "running distractor" to data platform, Master Lu started the road of evolution
猜你喜欢
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
Subghz, lorawan, Nb IOT, Internet of things
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
cf:C. Column Swapping【排序 + 模擬】
PTA ladder game exercise set l2-004 search tree judgment
Career experience feedback to novice programmers
Sequential storage of stacks
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
Jinfo of JVM command: view and modify JVM configuration parameters in real time
Detailed explanation of platform device driver architecture in driver development
随机推荐
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
JVM monitoring and diagnostic tools - command line
深度聚类:将深度表示学习和聚类联合优化
Question 102: sequence traversal of binary tree
CMD permanently delete specified folders and files
Cloud acceleration helps you effectively solve attack problems!
Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
进程间通信之共享内存
高并发大流量秒杀方案思路
cf:C. Column Swapping【排序 + 模拟】
软件测试的几个关键步骤,你需要知道
计算模型 FPS
DC-7靶机
CTFshow--常用姿势
Markdown 并排显示图片
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();
go-microservice-simple(2) go-Probuffer
3428. 放苹果
Oracle迁移中关于大容量表使用数据泵(expdp、impdp)导出导入容易出现的问题和注意事项
C note 13