当前位置:网站首页>对称的二叉树【树的遍历】
对称的二叉树【树的遍历】
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 ;
}
边栏推荐
- 【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
- Add salt and pepper noise or Gaussian noise to the picture
- The solution of a simple algebraic problem
- 牙齿干细胞的存储问题(未完待续)
- JVM命令之 jinfo:实时查看和修改JVM配置参数
- On the discrimination of "fake death" state of STC single chip microcomputer
- yarn入门(一篇就够了)
- [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]
- Introduction to yarn (one article is enough)
- 基于ADAU1452的DSP及DAC音频失真分析
猜你喜欢

laravel 使用腾讯云 COS5全教程

高并发大流量秒杀方案思路

如果不知道这4种缓存模式,敢说懂缓存吗?

yarn入门(一篇就够了)

Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou

Vscode for code completion

Jinfo of JVM command: view and modify JVM configuration parameters in real time

JVM监控及诊断工具-命令行篇

go-microservice-simple(2) go-Probuffer

Apple CMS V10 template /mxone Pro adaptive film and television website template
随机推荐
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
改变ui组件原有样式
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
C note 13
Peripheral driver library development notes 43: GPIO simulation SPI driver
JVM命令之 jstat:查看JVM統計信息
mac版php装xdebug环境(m1版)
蚂蚁庄园安全头盔 7.8蚂蚁庄园答案
[云原生]微服务架构是什么?
Career experience feedback to novice programmers
【GNN】图解GNN: A gentle introduction(含视频)
980. Different path III DFS
Reading notes of Clickhouse principle analysis and Application Practice (6)
linear regression
cf:C. Column Swapping【排序 + 模擬】
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
Cf:c. column swapping [sort + simulate]
Nvisual network visualization
Convert numbers to string strings (to_string()) convert strings to int sharp tools stoi();