当前位置:网站首页>对称的二叉树【树的遍历】
对称的二叉树【树的遍历】
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 ;
}
边栏推荐
- Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
- Jinfo of JVM command: view and modify JVM configuration parameters in real time
- What is make makefile cmake qmake and what is the difference?
- Cf:c. column swapping [sort + simulate]
- 高并发大流量秒杀方案思路
- 693. 行程排序
- jvm命令之 jcmd:多功能命令行
- 云加速,帮助您有效解决攻击问题!
- Vscode for code completion
- Change the original style of UI components
猜你喜欢

Say sqlyog deceived me!

你不知道的互联网公司招聘黑话大全

基于ADAU1452的DSP及DAC音频失真分析

Ctfshow-- common posture

测试开发基础,教你做一个完整功能的Web平台之环境准备

Rk3399 platform development series explanation (WiFi) 5.52. Introduction to WiFi framework composition

Introduction to yarn (one article is enough)

Jcmd of JVM command: multifunctional command line

How to improve website weight

Career experience feedback to novice programmers
随机推荐
职场经历反馈给初入职场的程序员
PowerPivot - DAX (function)
ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
一名普通学生的大一总结【不知我等是愚是狂,唯知一路向前奔驰】
Understand the deserialization principle of fastjson for generics
外设驱动库开发笔记43:GPIO模拟SPI驱动
老板总问我进展,是不信任我吗?(你觉得呢)
Nvisual network visualization
JVM monitoring and diagnostic tools - command line
Change the original style of UI components
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
当我们谈论不可变基础设施时,我们在谈论什么
Qt多线程的多种方法之一 QThread
On the discrimination of "fake death" state of STC single chip microcomputer
JVM command - jmap: export memory image file & memory usage
cf:C. Column Swapping【排序 + 模擬】
JVM命令之 jstat:查看JVM统计信息
测试开发基础,教你做一个完整功能的Web平台之环境准备
Rk3399 platform development series explanation (WiFi) 5.53, hostapd (WiFi AP mode) configuration file description
693. 行程排序