当前位置:网站首页>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
边栏推荐
- Nine abilities of agile manufacturing in the era of meta universe
- Custom login failure handling
- [COCI] Vje š TICA (subset DP)
- SQL tuning series - Introduction to SQL tuning
- 2022 mobile crane driver test question bank simulation test platform operation
- CSP certification 202203-2 travel plan (multiple solutions)
- DBA common SQL statements (3) - cache, undo, index and wait events
- 杰理之有时候定位到对应地址的函数不准确怎么办?【篇】
- 防疫登记小程序
- Sim Api User Guide(8)
猜你喜欢

2022年制冷与空调设备运行操作考试练习题及模拟考试

Cloud identity is too loose, opening the door for attackers

構建元宇宙時代敏捷制造的九種能力

Juc并发编程09——Condition实现源码分析

解决VMware卸载后再安装出现的问题

ARM调试(1):两种在keil中实现printf重定向到串口的方法

Redis design and Implementation

中控学习型红外遥控模块支持网络和串口控制

Educational Codeforces Round 81 (Rated for Div. 2)

2022 mobile crane driver test question bank simulation test platform operation
随机推荐
Career planning and implementation in the era of meta universe
《Redis设计与实现》
Formattime timestamp format conversion
第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
2022茶艺师(初级)考试试题模拟考试平台操作
通过流式数据集成实现数据价值(2)
Es aggregation aggregation analysis
Sim Api User Guide(8)
杰理之栈溢出 stackoverflow 怎么办?【篇】
Computer network security experiment II DNS protocol vulnerability utilization experiment
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
Nine abilities of agile manufacturing in the era of meta universe
[CF 1425d] danger of mad snakes
使用IDEA开发Spark程序
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
通过流式数据集成实现数据价值(5)- 流分析
第三章 启用和调整IM列存储的大小(IM-3.1)
SQL tuning series - SQL performance methodology
MapReduce压缩
Realize data value through streaming data integration (1)