当前位置:网站首页>力扣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;
}
边栏推荐
- Graffiti intelligence is listed on the dual main board in Hong Kong: market value of 11.2 billion Hong Kong, with an annual revenue of 300 million US dollars
- Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
- Dark horse -- redis
- Spark foundation -scala
- LeetCode_双指针_中等_61. 旋转链表
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
- R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the palette parameter, and set the colors of data points and box graphs of dot plots at differ
- Detailed idea and code implementation of infix expression to suffix expression
- 受益匪浅,安卓面试问题
- Black Horse - - Redis Chapter
猜你喜欢

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries

反射及在运用过程中出现的IllegalAccessException异常

Reflection and illegalaccessexception exception during application

包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链

Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview

【计算情与思】扫地僧、打字员、信息恐慌与奥本海默

凤凰架构3——事务处理

Druid database connection pool details

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

spark基础-scala
随机推荐
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
JDBC details
RT-Thread 组件 FinSH 使用时遇到的问题
【翻译】供应链安全项目in-toto移至CNCF孵化器
接雨水问题解析
short i =1; i=i+1与short i=1; i+=1的区别
English topic assignment (25)
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Black Horse - - Redis Chapter
Mysql Information Schema 学习(二)--Innodb表
zabbix 代理服务器 与 zabbix-snmp 监控
LeetCode-1279. Traffic light intersection
Benefit a lot, Android interview questions
Interview assault 63: how to remove duplication in MySQL?
在解决了 2961 个用户反馈后,我做出了这样的改变...
学习探索-函数防抖
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
Mathematical knowledge -- code implementation of Gaussian elimination (elementary line transformation to solve equations)
Don't miss this underestimated movie because of controversy!
[translation] supply chain security project in toto moved to CNCF incubator