当前位置:网站首页>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
边栏推荐
- Comparative analysis of meta universe from the dimension of knowledge dissemination
- 2022年流动式起重机司机考试题库模拟考试平台操作
- Sim Api User Guide(4)
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- 杰理之AES能256bit吗【篇】
- Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
- [lnoi2014] LCA - tree chain subdivision - multipoint LCA depth and problems
- DBA common SQL statements (1) - overview information
- ES-aggregation聚合分析
猜你喜欢
随机推荐
"Gu Yu series" airdrop
101. Symmetric Tree
CSP certification 202203-2 travel plan (multiple solutions)
Chapter II in memory architecture (im-2.2)
Yarn核心参数配置
Number theory blocking (integer division blocking)
Educational Codeforces Round 81 (Rated for Div. 2)
[COCI] Vje š TICA (subset DP)
DBA常用SQL语句 (5) - Latch 相关
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
转:毛姆:阅读是一座随身携带的避难所
Odoo server setup notes
【无标题】
Realize data value through streaming data integration (3) - real-time continuous data collection
代码源每日一题 div1 (701-707)
MapReduce计算流程详解
Rain produces hundreds of valleys, and all things grow
Formattime timestamp format conversion
CSP认证 202203-2 出行计划(多种解法)
PHP two-dimensional array specifies that the elements are added after they are equal, otherwise new
![[COCI] lattice (dichotomy + tree divide and conquer + string hash)](/img/7b/fe2a45d960a6d3eb7dc25200304adc.png)





![[untitled]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)


