当前位置:网站首页>力扣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;
}
边栏推荐
- 冒烟测试怎么做
- 思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
- Leetcode 30. 串联所有单词的子串
- 深入分析,Android面试真题解析火爆全网
- 学习探索-使用伪元素清除浮动元素造成的高度坍塌
- 包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
- Interview assault 63: how to remove duplication in MySQL?
- Detailed idea and code implementation of infix expression to suffix expression
- 助力安全人才专业素养提升 | 个人能力认证考核第一阶段圆满结束!
- Translation D28 (with AC code POJ 26:the nearest number)
猜你喜欢
Detailed idea and code implementation of infix expression to suffix expression
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
打家劫舍III[后序遍历与回溯+动态规划]
It's super detailed in history. It's too late for you to read this information if you want to find a job
Druid 数据库连接池 详解
接雨水问题解析
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
ROS custom message publishing subscription example
史上超级详细,想找工作的你还不看这份资料就晚了
随机推荐
MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
Synchronous development of business and application: strategic suggestions for application modernization
Detailed idea and code implementation of infix expression to suffix expression
在解决了 2961 个用户反馈后,我做出了这样的改变...
A popular explanation will help you get started
Test technology stack arrangement -- self cultivation of test development engineers
DaGAN论文解读
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
C language daily practice - day 22: Zero foundation learning dynamic planning
Modulenotfounderror: no module named 'PIL' solution
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
【翻译】供应链安全项目in-toto移至CNCF孵化器
LeetCode_双指针_中等_61. 旋转链表
A method of removing text blur based on pixel repair
学习探索-使用伪元素清除浮动元素造成的高度坍塌
1805. 字符串中不同整数的数目
反射及在运用过程中出现的IllegalAccessException异常
Interface test tool - postman
Using clip path to draw irregular graphics