当前位置:网站首页>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;
}
边栏推荐
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- [translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
- 算法面试经典100题,Android程序员最新职业规划
- Translation D28 (with AC code POJ 26:the nearest number)
- How to access localhost:8000 by mobile phone
- spark基础-scala
- 接雨水问题解析
- [pytorch] yolov5 train your own data set
- How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!
- 力扣101题:对称二叉树
猜你喜欢

Don't miss this underestimated movie because of controversy!

The second day of rhcsa study

Cereals Mall - Distributed Advanced p129~p339 (end)

LeetCode-1279. 红绿灯路口

LeetCode_双指针_中等_61. 旋转链表

社招面试心得,2022最新Android高频精选面试题分享
Interview assault 63: how to remove duplication in MySQL?

Hudi vs Delta vs Iceberg

C language daily practice - day 22: Zero foundation learning dynamic planning

PMP practice once a day | don't get lost in the exam -7.6
随机推荐
【基础架构】Flink/Flink-CDC的部署和配置(MySQL / ES)
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
LeetCode_双指针_中等_61. 旋转链表
JDBC详解
利用 clip-path 绘制不规则的图形
English topic assignment (25)
关于图像的读取及处理等
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Benefit a lot, Android interview questions
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
The list of people who passed the fifth phase of personal ability certification assessment was published
Looting iii[post sequence traversal and backtracking + dynamic planning]
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
An error occurs when installing MySQL: could not create or access the registry key needed for the
A method of removing text blur based on pixel repair
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe