当前位置:网站首页>Sword finger offer 28 Symmetric binary tree
Sword finger offer 28 Symmetric binary tree
2022-06-22 02:49:00 【SS_ zico】
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
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
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
bool compare(TreeNode *left,TreeNode *right)
{
if(!left&&!right)return true;
if(left == NULL || right == NULL || left->val!=right->val)return false;
return compare(left->left,right->right)&&compare(left->right,right->left);
}
bool isSymmetric(TreeNode* root) {
return root == NULL ? true : compare(root->left,root->right);
}
};
Time complexity O(N) : Call at most N/2 Time compare() Method .
Spatial complexity O(N) : The worst , A binary tree degenerates into a linked list , System use O(N) The size of the stack space .
边栏推荐
- Use of day19qpushbutton 2021-10-30
- Game Jam开发周期
- How to apply PMP project management knowledge?
- PostgreSQL fetches data according to the size of the time field
- 从数据库的分类说起,一文了解图数据库
- Which Amazon evaluation system is better?
- Common string operations in day15qt 2021-10-20
- 华阳智能冲刺深交所:拟募资4亿 复星惟盈是股东
- 并查集dsu
- import和require在浏览器和node环境下的实现差异
猜你喜欢

EMC輻射發射整改-原理案例分析

JVM makes wheels

Introduction to Apache ActiveMQ Artemis

国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了

关于PMP考试,你想知道的知识都在这里了

Starting from the classification of database, I understand the graph database

基于xposed框架hook使用

Unicode decodeerror appears: 'ASCII' codec can't decode byte 0xe9 in position 0: ordinal not in range solution
![[proteus simulation] INT0 and INT1 interrupt count](/img/0b/b3f5adb97046d927e501ea34deb3d9.png)
[proteus simulation] INT0 and INT1 interrupt count

Using neo4j sandbox to learn neo4j graph data science GDS
随机推荐
【4. 高精度加法】
xpm_ memory_ A complete example of using the tdpram primitive
EMC輻射發射整改-原理案例分析
ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
【2. 归并排序】
Write your own kubernetes controller
2022 brazing test simulation 100 questions and answers
360EDR刨析
国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
[9. submatrix sum]
All the knowledge you want to know about the PMP Exam is here
Penetration testing - logic vulnerability topic
最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA
理想L9正式发布:8月底前开始交付 零售价45.98万元
Day13QMainWindow2021-09-28
如何选择合适的 Neo4j 版本(2022版)
Graphacademy course explanation: Fundamentals of neo4j graph data science
Programming of pytorch interface
Introduction to Apache ActiveMQ Artemis
PostgreSQL fetches data according to the size of the time field