当前位置:网站首页>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
边栏推荐
- NEC infrared remote control coding description
- Realize data value through streaming data integration (1)
- 杰理之AES能256bit吗【篇】
- 2022茶艺师(初级)考试试题模拟考试平台操作
- Number theory blocking (integer division blocking)
- [hdu6833] a very easy math problem
- failureForwardUrl与failureUrl
- 打印页面的功能实现
- 从知识传播的维度对比分析元宇宙
- The central control learning infrared remote control module supports network and serial port control
猜你喜欢
Redis design and Implementation
MapReduce计算流程详解
《谷雨系列》空投
構建元宇宙時代敏捷制造的九種能力
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview
Nvidia最新三维重建技术Instant-ngp初探
Sim Api User Guide(5)
自定义登录失败处理
Juc并发编程09——Condition实现源码分析
随机推荐
Less than 100 secrets about prime numbers
[educational codeforces round 80] problem solving Report
Compile and debug mysql8 with clion under MacOS x
Juc并发编程09——Condition实现源码分析
通过流式数据集成实现数据价值(5)- 流分析
Odoo 服务器搭建备忘
DBA常用SQL语句(6)- 日常管理
Explanation of order and primitive root of number theory
工业元宇宙平台规划与建设
Odoo server setup notes
MapReduce核心和基础Demo
[untitled]
P1390 sum of common divisor (Mobius inversion)
Redis design and Implementation
DBA常用SQL语句 (5) - Latch 相关
DBA常用SQL语句(3)- cache、undo、索引和等待事件
Skill point digging
Number theory blocking (integer division blocking)
2022年流动式起重机司机考试题库模拟考试平台操作
元宇宙时代的职业规划与执行