当前位置:网站首页>101. Symmetric binary tree
101. Symmetric binary tree
2022-07-20 03:21:00 【ZNineSun】
Punch in one question every day !!!
Today I want to share with you a topic of recursive type .
Title Description :
Give you the root node of a binary tree root , Check whether it is axisymmetric .
Title Example :
Whether it is axisymmetric , In fact, let's judge whether the subtrees on the left and right sides are equal .
In a little bit more depth , In fact, let us judge
- left The left node of and right Whether the right nodes of are equal
- left The right node of and right Whether the left nodes of are equal .
I use the example in the title 1 For example , On the left side of the second floor 2 This node , Its left node =3;
On the right side of the second floor 2 This node , Its right node =3; At present, it still conforms to symmetry , Let's continue to judge downward :
On the left side of the second floor 2 This node , Its right node =4;
On the right side of the second floor 2 This node , Its left node =4;
All meet the requirements of symmetry , It means that the third layer is symmetrical , If there is a third layer 、 The fourth level 、 The fifth floor … We can continue to judge downward according to this logic .
Code :(PS:Java edition )
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return dfs(root.left, root.right);
}
public boolean dfs(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null) {
return false;
}
if (left.val != right.val) {
return false;
}
return dfs(left.left, right.right) && dfs(left.right, right.left);
}
边栏推荐
猜你喜欢
试水茶饮的MANNER,想靠阿嬷走出上海?
Capability verification and planning capability of performance test
Understand Yolo V4 in one article
opencv学习 Resize and Crop
YOLOv5改进:添加注意力机制(视频教程)
AIRIOT物联网平台赋能集装箱行业构建焊接工位信息监控系统
Using applet runtime technology to enhance the cross end development attribute of flutter
Compose Canvas 自定义圆形进度条
DataX theory of data synchronization tools
升级生态主张:阿里云为千行百业带来了什么?
随机推荐
贵金属知识:夺命短线秘籍
改善用户体验的404页面最佳实践
Query data with DQL
现在的元宇宙早已不再是一个仅仅只是被应用于社交领域的存在
良品铺子迷失高端化
Capability verification and planning capability of performance test
Flink directory Guide
在线SQL转文本工具
FPN网络详解
飞盘火了,VC开始投瑜伽裤
Selenium: positioning (II: by module positioning, simple without foundation)
Interview series
爬虫数据解析
SSM framework integration (necessary for beginners)
ICML2022论文太多看不过来?这份《一句话点评1234篇论文亮点》帮你快速找到想看的
Basic concepts of neural network model
论文精读:Mask R-CNN
碳中和+Web3,比尔•盖茨会投么?
SQL Server 所依赖的磁盘已接近满,速度很慢该如何解决
Introduction and advanced tutorial of deep learning