当前位置:网站首页>Li Kou 101: symmetric binary tree
Li Kou 101: symmetric binary tree
2022-07-06 19:36:00 【Yingtai night snow】
Power button 101 topic : Symmetric binary tree
Title Description
Give you the root node of a binary tree root , Check whether it is axisymmetric .
I/o sample

Input :root = [1,2,2,3,4,4,3]
Output :true

Input :root = [1,2,2,null,3,null,3]
Output :false
Algorithm one , Using recursion
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;
}
Algorithm 2 , Use the official simpler recursion
bool check(TreeNode *root1,TreeNode *root2)
{
if(!root1&&!root2)
{
return true;
}
if(!root1||!root2)
{
return false;
}
// Combine the above conditional judgments
return root1->val==root2->val&&check(root1->left,root2->right)&&check(root1->right,root2->left);
}
// The official relatively simple recursive algorithm
bool isSymmetric3(TreeNode *&root)
{
return check(root,root);
}
Algorithm three , Use queues to implement iterations
// Solution 3 , Use the idea of iteration , Use the queue to judge
bool isSymmetric4(TreeNode *&root)
{
if(!root)
{
return true;
}
if (!root->left&&!root->right)
{
return true;
}
// Create a queue save node
queue<TreeNode *>que;
// Join the left child node and the right child node of the root node
que.push(root->left);
que.push(root->right);
// The existence of the next node is not verified , Only verify whether the current node exists
while(!que.empty())
{
// Create a temporary node to save the current node
TreeNode *tempLeft=que.front();
que.pop();
TreeNode *tempRight=que.front();
que.pop();
// When both nodes are empty , Continue to cycle , If one is empty, it returns false
if(!tempLeft&&!tempRight)
{
continue;
}
if((!tempLeft||!tempRight)||(tempLeft->val!=tempRight->val))
{
return false;
}
// Queue the values of two nodes respectively
que.push(tempLeft->left);
que.push(tempRight->right);
que.push(tempLeft->right);
que.push(tempRight->left);
}
return true;
}
边栏推荐
- Teach you to learn JS prototype and prototype chain hand in hand, a tutorial that monkeys can understand
- [translation] supply chain security project in toto moved to CNCF incubator
- Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
- 三面蚂蚁金服成功拿到offer,Android开发社招面试经验
- LeetCode-1279. Traffic light intersection
- spark基础-scala
- Leetcode topic [array] - 119 Yang Hui triangle II
- 数学知识——高斯消元(初等行变换解方程组)代码实现
- Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference
- 10 schemes to ensure interface data security
猜你喜欢

RT-Thread 组件 FinSH 使用时遇到的问题

Swiftui game source code Encyclopedia of Snake game based on geometryreader and preference

零基础入门PolarDB-X:搭建高可用系统并联动数据大屏

Reflection and illegalaccessexception exception during application

MySQL information Schema Learning (i) - - General table

LeetCode-1279. 红绿灯路口

Detailed idea and code implementation of infix expression to suffix expression

腾讯Android面试必问,10年Android开发经验

蓝桥杯 微生物增殖 C语言

全套教学资料,阿里快手拼多多等7家大厂Android面试真题
随机推荐
Interpretation of Dagan paper
LeetCode_双指针_中等_61. 旋转链表
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
MySQL information Schema Learning (i) - - General table
Yyds dry goods inventory leetcode question set 751 - 760
Computer network: sorting out common network interview questions (I)
The slave i/o thread stops because master and slave have equal MySQL serv
How to do smoke test
Chic Lang: attributeerror: partially initialized module 'CV2' has no attribute 'GAPI_ wip_ gst_ GStreamerPipe
The second day of rhcsa study
凤凰架构2——访问远程服务
Translation D28 (with AC code POJ 26:the nearest number)
学习探索-使用伪元素清除浮动元素造成的高度坍塌
凤凰架构3——事务处理
Solution of intelligent management platform for suppliers in hardware and electromechanical industry: optimize supply chain management and drive enterprise performance growth
About image reading and processing, etc
Phoenix Architecture 3 - transaction processing
利用 clip-path 绘制不规则的图形
Swagger2 reports an error illegal DefaultValue null for parameter type integer
1805. 字符串中不同整数的数目