当前位置:网站首页>Sword finger offer 28 Symmetric binary tree
Sword finger offer 28 Symmetric binary tree
2022-07-07 22:52:00 【Yes' level training strategy】
subject : 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 .
But the next one [1,2,2,null,3,null,3] It's not mirror symmetric :
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
According to the meaning , In fact, it is to compare the left and right subtrees .
First judge the root node's left Whether or not right equal , If so, continue recursive judgment , The objects of judgment are left.left and right.right、left.right and right.left.
In this way, recursive judgment in turn , The idea should be very clear , If you encounter any nonconformity in the middle, you can return directly .
Let's take a look at the code :
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return isSymmetric(root.left, root.right);
}
private boolean isSymmetric(TreeNode left, TreeNode right) {
if ((left == null && right != null) || (left != null && right == null)) {
return false;
}
if (left == null && right == null) {
return true;
}
if (left.val != right.val) {
return false;
}
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
}
}
This is the most obvious and direct judgment , therefore isSymmetric The code can be optimized :
class Solution {
private boolean isSymmetric(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
if (left == null || right == null || left.val != right.val) {
return false;
}
return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left);
}
}
Time complexity O(N), Spatial complexity O(N)
Okay , A simple question !
https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/
边栏推荐
- 6-3 find the table length of the linked table
- LeetCode707. Design linked list
- OpenGL job - texture
- Blender exchange group, welcome to the water group ~
- Unity technical notes (II) basic functions of scriptableobject
- Remove the default background color of chrome input input box
- 微服務遠程Debug,Nocalhost + Rainbond微服務開發第二彈
- IP网络主动测评系统——X-Vision
- Px4 autonomous flight
- Cannot find module 'xxx' or its corresponding type declaration
猜你喜欢
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
How pyGame rotates pictures
Redis集群安装
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
Digital transformation: five steps to promote enterprise progress
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
VTOL in Px4_ att_ Control source code analysis [supplement]
UWA Q & a collection
Loki, the "open source star picking program", realizes the efficient management of harbor logs
随机推荐
Revit secondary development - intercept project error / warning pop-up
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Ren Qian code compilation error modification
How to close eslint related rules
This experimental syntax requires enabling the parser plugin: ‘optionalChaining‘
行测-图形推理-9-线条问题类
Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
行测-图形推理-6-相似图形类
OpenGL homework - Hello, triangle
详解全志V853上的ARM A7和RISC-V E907之间的通信方式
Two methods of calling WCF service by C #
Revit secondary development - operation family documents
行测-图形推理-2-黑白格类
行测-图形推理-8-图群类
Record layoutrebuild Forcerebuildlayoutimmediate does not take effect
Ligne - raisonnement graphique - 4 - classe de lettres
Revit secondary development - cut view
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
筑起云端 “免疫”屏障,让你的数据有备无患