当前位置:网站首页>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);
}
}
边栏推荐
- 用Unity不会几个插件怎么能行?Unity各类插件及教程推荐
- Definition of information entropy
- [flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)
- DNS分离解析的实现方法详解
- There is a newline problem when passing shell script parameters \r
- SQL语言(四)
- 圆角大杀器,使用滤镜构建圆角及波浪效果!
- Learn NLP with Transformer (Chapter 5)
- JS convert pseudo array to array
- 同事看了我的代码惊呼:居然是这么在Unity中用单例的
猜你喜欢

SQL语言(二)

Learn PHP -- phpstudy tips mysqld Exe: Error While Setting Value ‘NO_ ENGINE_ Solution of substitution error

学习路之PHP--TP5.0使用中文当别名,报“不支持的数据表达式”
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/c8/fe18308ddad5cb2fbccb79d2d8a2b6.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training

SQL language (II)

SQL注入 Less17(报错注入+子查询)
Learn NLP with Transformer (Chapter 2)

Implementation of recommendation system collaborative filtering in spark

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

游戏背包系统,“Inventory Pro插件”,研究学习-----妈妈再也不用担心我不会做背包了(Unity3D)
随机推荐
Signal integrity (SI) power integrity (PI) learning notes (XXXIII) 102 general design rules to minimize signal integrity problems
Stm32cubemx learning record -- installation, configuration and use
MLX90640 红外热成像仪测温模块开发笔记(五)
信息熵的定义
There is a newline problem when passing shell script parameters \r
[动态规划] 70. 爬楼梯
leetcode 剑指 Offer 28. 对称的二叉树
[flask advanced] solve the classic error reporting of flask by combining the source code: working outside of application context
HCIA experiment (09)
同事看了我的代码惊呼:居然是这么在Unity中用单例的
Mlx90640 infrared thermal imager temperature measurement module development notes (V)
MySQL | GROUP_ The concat function concatenates the values of a column with commas
Detailed explanation of zero basis from macro to micro Bert
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
HDD Hangzhou station full experience
SQL注入 Less17(报错注入+子查询)
Shell - Chapter 6 exercise
Understanding: idea uses Scala to write wordcount programs and generate jar packages
leetcode 剑指 Offer 27. 二叉树的镜像