当前位置:网站首页>Leetcode sword finger offer 28. symmetric binary tree
Leetcode sword finger offer 28. symmetric binary tree
2022-07-25 11:29:00 【kt1776133839】
Title Description :
Please implement a function , Used to judge whether a binary tree is symmetrical . If a binary tree is the same as its mirror image , So it's symmetrical .
for example , Binary tree [1,2,2,3,4,4,3] It's symmetrical .
1
/ \
2 2
/ \ / \
3 4 4 3
But the next one [1,2,2,null,3,null,3] It's not mirror symmetric :
1
/ \
2 2
\ \
3 3
Examples :
Example 1:
Input :root = [1,2,2,3,4,4,3]
Output :true
Example 2:
Input :root = [1,2,2,null,3,null,3]
Output :false
Limit :
0 <= Number of nodes <= 1000
Their thinking :
Symmetric binary tree definition : For trees Any two symmetric nodes L and R , There must be :
L.val=R.val : That is, the values of these two symmetric nodes are equal .
L.left.val=R.right.val : namely L Of The left child node and R Of The right child node symmetry ;
L.right.val=R.left.val: namely L Of The right child node and R Of The left child node symmetry .
According to the above rule , Consider recursion from top to bottom , Judge whether each pair of nodes is symmetrical , So as to judge whether the tree is a symmetric binary tree .

Java Program :
class Solution {
public boolean isSymmetric(TreeNode root) {
return root == null ? true : recur(root.left, root.right);
}
boolean recur(TreeNode L, TreeNode R) {
if(L == null && R == null) return true;
if(L == null || R == null || L.val != R.val) return false;
return recur(L.left, R.right) && recur(L.right, R.left);
}
}
边栏推荐
- Hcip experiment (01)
- 学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
- From the perspective of open source, analyze the architecture design of SAP classic ERP that will not change in 30 years
- 玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
- PostgreSQL踩坑 | ERROR: operator does not exist: uuid = character varying
- Nowcodertop12-16 - continuous updating
- Learn NLP with Transformer (Chapter 3)
- ArcMap无法启动解决方法
- [flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
- SQL language (I)
猜你喜欢

The most detailed MySQL index analysis (mind map is attached at the end of the article)

Hcip experiment (01)

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots

NowCoderTOP1-6——持续更新ing

Review recitation finishing version

Learn NLP with Transformer (Chapter 7)

复习背诵整理版

黑客入门教程(非常详细)从零基础入门到精通,看完这一篇就够了。

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)

新能源销冠宏光MINIEV,有着怎样的产品力?
随机推荐
syncronized锁升级的过程
Learn NLP with Transformer (Chapter 8)
爬虫基础一
SQL语言(二)
LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying
C# Newtonsoft.Json 高级用法
Hcip experiment (04)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
基于cornerstone.js的dicom医学影像查看浏览功能
A troubleshooting record of DirectShow playback problems
ESP8266 使用 DRV8833驱动板驱动N20电机
Shell fourth day homework
Nowcodertop1-6 - continuous updating
Hcip experiment (01)
HCIA experiment (07) comprehensive experiment
Implementation of recommendation system collaborative filtering in spark
STM32CubeMX学习记录--安装配置与使用
Smart cloud IOT platform STM32 esp8266-01s simple wireless light control
Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework