当前位置:网站首页>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 .
边栏推荐
- 【5. 高精度减法】
- UnionPay payment return merchant nignx post request 405
- Penetration testing - logic vulnerability topic
- discuz! Bug in the VIP plug-in of the forum repair station help network: when the VIP member expires and the permanent member is re opened, the user group does not switch to the permanent member group
- Wechat applet film and television review and exchange platform system graduation design (1) development outline
- EMC Radiation Emission rectification - principle Case Analysis
- Get to know unity3d (project structure, third-party plug-in of probuilder)
- All the knowledge you want to know about the PMP Exam is here
- ATM机模拟系统
- 智翔金泰冲刺科创板:年营收3919万亏损超3亿 拟募资40亿
猜你喜欢

Unicode decodeerror appears: 'ASCII' codec can't decode byte 0xe9 in position 0: ordinal not in range solution

All the knowledge you want to know about the PMP Exam is here
![Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd](/img/4e/345ceb4e91aae844d7ab53ad3fd988.png)
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd

360EDR刨析

Asemi Schottky diode 1N5819 parameters, 1N5819 replacement, 1N5819 source

理想L9正式发布:8月底前开始交付 零售价45.98万元

Game Jam开发周期

最新发布:Neo4j 图数据科学 GDS 2.0 和 AuraDS GA

Technical exploration: 360 digital subjects won the first place in the world in ICDAR OCR competition

JS special effects in the construction of animated web pages
随机推荐
[8. One dimensional prefix and]
[proteus simulation] INT0 and INT1 interrupt count
Using neo4j sandbox to learn neo4j graph data science GDS
Wechat applet film and television review and exchange platform system graduation design (1) development outline
【8、一维前缀和】
import和require在浏览器和node环境下的实现差异
理想L9正式发布:8月底前开始交付 零售价45.98万元
Vscode custom template, take notes with the template?!
【1. 快速排序】
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd
Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder
UnionPay payment return merchant nignx post request 405
With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
Write your own kubernetes controller
Neo4j 技能树正式发布,助你轻松掌握Neo4j图数据库
File upload vulnerability shooting range analysis upload_ LABS
Starting WDA with tidevice
基于xposed框架hook使用
【7. 高精度除法】