当前位置:网站首页>力扣101题:对称二叉树
力扣101题:对称二叉树
2022-07-06 11:35:00 【瀛台夜雪】
力扣101题:对称二叉树
题目描述
给你一个二叉树的根节点 root
, 检查它是否轴对称。
输入输出样例
输入:root = [1,2,2,3,4,4,3]
输出:true
输入:root = [1,2,2,null,3,null,3]
输出:false
算法一,使用递归
bool rootIsSymmetric(TreeNode *root1,TreeNode *root2)
{
bool leftTrue=false,rightTrue=false;
if(root1->val==root2->val)
{
if(root1->left&&root2->right)
{
leftTrue=rootIsSymmetric(root1->left,root2->right);
}
else if(!root1->left&&!root2->right)
{
leftTrue=true;
}
if(root1->right&&root2->left)
{
rightTrue=rootIsSymmetric(root1->right,root2->left);
}
else if(!root1->right&&!root2->left)
{
rightTrue=true;
}
return leftTrue&&rightTrue;
}
return false;
}
bool isSymmetric2(TreeNode *&root)
{
if(!root)
{
return true;
}
if(root->right&&root->left)
{
if(root->left->val==root->left->val)
{
return rootIsSymmetric(root->left,root->right);
}
return false;
}
else if(!root->left&&!root->right)
{
return true;
}
return false;
}
算法二,使用官方较为精简的递归
bool check(TreeNode *root1,TreeNode *root2)
{
if(!root1&&!root2)
{
return true;
}
if(!root1||!root2)
{
return false;
}
//将上面的几个条件判断合并到一块去了
return root1->val==root2->val&&check(root1->left,root2->right)&&check(root1->right,root2->left);
}
//官方较为简洁的递归算法
bool isSymmetric3(TreeNode *&root)
{
return check(root,root);
}
算法三,使用队列实现迭代
//解法三,使用迭代的思想,借助队列进行判断
bool isSymmetric4(TreeNode *&root)
{
if(!root)
{
return true;
}
if (!root->left&&!root->right)
{
return true;
}
//建立队列保存结点
queue<TreeNode *>que;
//将根节点的左孩子结点和右孩子结点入队
que.push(root->left);
que.push(root->right);
//并未验证下一个结点是否存在,只验证当前的结点是否存在
while(!que.empty())
{
//建立临时结点保存当前的结点
TreeNode *tempLeft=que.front();
que.pop();
TreeNode *tempRight=que.front();
que.pop();
//当两个结点都为空,继续循环,有一个为空则返回false
if(!tempLeft&&!tempRight)
{
continue;
}
if((!tempLeft||!tempRight)||(tempLeft->val!=tempRight->val))
{
return false;
}
//将两个结点的值分别入队
que.push(tempLeft->left);
que.push(tempRight->right);
que.push(tempLeft->right);
que.push(tempRight->left);
}
return true;
}
边栏推荐
- 【翻译】供应链安全项目in-toto移至CNCF孵化器
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- LeetCode_格雷编码_中等_89.格雷编码
- 五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
- 学习探索-函数防抖
- ZABBIX proxy server and ZABBIX SNMP monitoring
- How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
- R language ggplot2 visualization: use the ggstripchart function of ggpubr package to visualize the grouped dot strip plot, and set the add parameter to add box plots for different levels of dot strip
- The list of people who passed the fifth phase of personal ability certification assessment was published
- LeetCode-1279. 红绿灯路口
猜你喜欢
Carte de réflexion + code source + notes + projet, saut d'octets + jd + 360 + tri des questions d'entrevue Netease
Looting iii[post sequence traversal and backtracking + dynamic planning]
面试突击63:MySQL 中如何去重?
Don't miss this underestimated movie because of controversy!
PMP practice once a day | don't get lost in the exam -7.6
Druid 数据库连接池 详解
An error occurs when installing MySQL: could not create or access the registry key needed for the
DaGAN论文解读
谷粒商城--分布式高级篇P129~P339(完结)
Mysql Information Schema 学习(一)--通用表
随机推荐
Synchronous development of business and application: strategic suggestions for application modernization
The second day of rhcsa study
An error occurs when installing MySQL: could not create or access the registry key needed for the
IC设计流程中需要使用到的文件
[pytorch] yolov5 train your own data set
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
冒烟测试怎么做
Simple understanding of MySQL database
Unbalance balance (dynamic programming, DP)
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
Mysql Information Schema 學習(一)--通用錶
Analysis of frequent chain breaks in applications using Druid connection pools
[translation] supply chain security project in toto moved to CNCF incubator
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
How can my Haskell program or library find its version number- How can my Haskell program or library find its version number?
LeetCode_格雷编码_中等_89.格雷编码
保证接口数据安全的10种方案
Spark foundation -scala
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。