当前位置:网站首页>101. Symmetric Tree
101. Symmetric Tree
2022-04-23 10:03:00 【soO_ 007】
subject :
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:

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

Input: root = [1,2,2,null,3,null,3] Output: false
Constraints:
- The number of nodes in the tree is in the range
[1, 1000]. -100 <= Node.val <= 100
Ideas 1:
Judge whether a tree is mirror symmetrical . First use bfs Here comes the stupid but easy to understand idea . On the basis of traversal by layer , Add a save Treenode* Of vector, Save all the children of the current layer , Including loopholes , And then put this vector Symmetrical comparison . The comparison is divided into five cases , Suppose the one on the left node by com[i], Dexter node by com[j]: First, left and right are empty , Then symmetrical ; Second, left space is not empty , Asymmetry ; Third, the left is not empty, the right is empty , Asymmetry ; Fourth, the left and right are not empty, but the values are not equal , Asymmetry ; Fifth, the left and right are not empty and the values are equal , Then symmetrical . As long as it is asymmetric , Go straight back to false that will do . This one more vector The solution is easy to understand , Is the complexity of taking up more space .
Code 1:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
while(q.size()) {
int len = q.size();
vector<TreeNode*> com;
for (int i = 0; i < len; i++) {
auto tmp = q.front();
q.pop();
com.push_back(tmp->left);
com.push_back(tmp->right);
if (tmp->left)
q.push(tmp->left);
if (tmp->right)
q.push(tmp->right);
}
for (int i = 0, j = com.size() - 1; i < com.size() / 2; i++, j--) {
if (!com[i] && !com[j])
continue;
if (com[i] && com[j] && com[i]->val == com[j]->val)
continue;
return false;
}
}
return true;
}
};
Ideas 2:
dfs How to write it , We know the five situations discussed above , That's it base case. Asymmetric situations return directly to false; And when the left and right are empty , We don't need to recurse down , Therefore, you can also directly return ture, The last case is that the left and right are not empty and the values are equal , We continue to recurse . We need to compare the outer and inner subtrees , That is, the left child of the left child should be the same as the right child of the right child , The right child of the left child is the same as the left child of the right child , Then return the results of both && that will do .
Code 2:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return dfs(root->left, root->right);
}
private:
bool dfs(TreeNode* left, TreeNode* right) {
if (!left && !right)
return true;
else if (left && !right)
return false;
else if (!left && right)
return false;
else if (left->val != right->val)
return false;
bool waice = dfs(left->left, right->right);
bool neice = dfs(left->right, right->left);
return waice && neice;
}
};
版权声明
本文为[soO_ 007]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230956497715.html
边栏推荐
猜你喜欢

0704、ansible----01

Cloud identity is too loose, opening the door for attackers

Easy to understand subset DP

2022年流动式起重机司机考试题库模拟考试平台操作

Planning and construction of industrial meta universe platform

2022年广东省安全员A证第三批(主要负责人)考试试题及答案

Number theory blocking (integer division blocking)
![[COCI] lattice (dichotomy + tree divide and conquer + string hash)](/img/7b/fe2a45d960a6d3eb7dc25200304adc.png)
[COCI] lattice (dichotomy + tree divide and conquer + string hash)

Depth selector

杰理之更准确地确定异常地址【篇】
随机推荐
Sim Api User Guide(5)
Odoo server setup notes
Juc并发编程06——深入剖析队列同步器AQS源码
Realizing data value through streaming data integration (4) - streaming data pipeline
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
Realizing data value through streaming data integration (5) - stream processing
[hdu6868] absolute math (pusher + Mobius inversion)
【无标题】
[lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
C language: expression evaluation (integer promotion, arithmetic conversion...)
2022 mobile crane driver test question bank simulation test platform operation
通过流式数据集成实现数据价值(3)- 实时持续数据收集
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
Prefix sum of integral function -- Du Jiao sieve
DBA common SQL statements (2) - SGA and PGA
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
Cloud identity is too loose, opening the door for attackers
DBA常用SQL语句(4)- Top SQL
C语言:表达式求值(整型提升、算术转换 ...)
SQL tuning series - Introduction to SQL tuning