当前位置:网站首页>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/
边栏推荐
- Unity local coordinates and world coordinates
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- Failed to initialize rosdep after installing ROS
- Line test - graphic reasoning - 3 - symmetric graphic class
- Debezium series: binlogreader for source code reading
- Visual studio 2019 installation
- Leetcode94. Middle order traversal of binary trees
- Digital transformation: five steps to promote enterprise progress
- [interview arrangement] 0211 game engine server
- Unity technical notes (II) basic functions of scriptableobject
猜你喜欢

行测-图形推理-8-图群类

LeetCode203. Remove linked list elements

Microservice Remote debug, nocalhost + rainbond microservice Development second Bomb

ASP.NET Core入门五

C # realizes the communication between Modbus protocol and PLC

0-5vac to 4-20mA AC current isolated transmitter / conversion module

「开源摘星计划」Loki实现Harbor日志的高效管理

Visual studio 2019 installation
![[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
![LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]](/img/5e/e442c8649b9123a9d9df7c0d61a564.jpg)
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
随机推荐
Leetcode94. Middle order traversal of binary trees
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Typeorm automatically generates entity classes
Failed to initialize rosdep after installing ROS
Antd date component appears in English
Xcode modifies the default background image of launchscreen and still displays the original image
Debezium series: support the use of variables in the Kill Command
7-18 simple simulation of banking business queue
Install mxnet GPU version
Remove the default background color of chrome input input box
Ueeditor custom display insert code
Ligne - raisonnement graphique - 4 - classe de lettres
Leetcode1984. Minimum difference in student scores
行测-图形推理-8-图群类
IP network active evaluation system -- x-vision
Two methods of calling WCF service by C #
Get the exact offset of the element
Redis集群安装
vite Unrestricted file system access to
How to judge whether the input content is "number"