当前位置:网站首页>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);
}
}
边栏推荐
- ArcMap cannot start the solution
- 玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧
- 大话DevOps监控,团队如何选择监控工具?
- Dataframe print 省略号问题
- The most detailed MySQL index analysis (mind map is attached at the end of the article)
- STM32CubeMX学习记录--安装配置与使用
- HCIP (01)
- Shell 脚本参数传递时有 \r 换行符问题
- [flask advanced] deeply understand the application context and request context of flask from the source code
- 常见WEB攻击与防御
猜你喜欢

PostgreSQL stepping on the pit | error: operator does not exist: UUID = character varying

SQL language (I)

Reinforcement learning (III)

玩游戏想记录一下自己超神的瞬间?那么就来看一下如何使用Unity截图吧

只知道预制体是用来生成物体的?看我如何使用Unity生成UI预制体

常见WEB攻击与防御

SQL注入 Less23(过滤注释符)
Learn NLP with Transformer (Chapter 4)

用Unity不会几个插件怎么能行?Unity各类插件及教程推荐

史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
随机推荐
SQL language (I)
Hcip experiment (04)
工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖
常见的几种PCB表面处理技术!
游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
tensorflow 调用多块GPU的一些错误
SQL注入 Less18(头部注入+报错注入)
What is MySQL transaction
[tree] 100. Same tree
全网显示 IP 归属地,是怎么实现的?
HCIA experiment (08)
新能源销冠宏光MINIEV,有着怎样的产品力?
What kind of product power does Hongguang miniev, the top seller of new energy, have?
MLX90640 红外热成像仪测温模块开发笔记(五)
MySQL master-slave replication and read-write separation
shell-第六章练习
史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
HCIP(11)
Learn NLP with Transformer (Chapter 4)