当前位置:网站首页>101. Symmetric Tree
101. Symmetric Tree
2022-04-23 09:57:00 【soO_007】
题目:
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
思路1:
判断一棵树是不是镜像对称。先用bfs里比较笨但是好理解的思路来了一遍。在按层遍历的基础上,增加一个存Treenode*的vector,把当前层的所有子都存进去,包括空子,然后将这个vector对称比较。比较分五种情况,假设左边的node为 com[i], 右边的node为com[j]:一是左右都为空,则对称;二是左空又不空,不对称;三是左不空右空,不对称;四是左右都不空但值不相等,不对称;五是左右都不空且值相等,则对称。只要是不对称的情况,直接返回false即可。这种多一个vector的解法比较好理解,就是多占空间复杂度。
代码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;
}
};
思路2:
dfs写法,我们已知上面讨论了的五种情况,那就是base case。不对称的情况直接返回false;而左右都空的情况下,我们是不需要往下递归的,因此也可以直接返回ture,最后一种左右不为空且值相等的情况,我们才继续递归。我们需要比较的外侧和内侧的子树,即左子的左子要和右子的右子相同,左子的右子和右子的左子相同,之后返回这两者结果的&&即可。
代码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://blog.csdn.net/weixin_49991368/article/details/124334486
边栏推荐
猜你喜欢

Sim Api User Guide(5)

Alibaba cloud architects interpret the four mainstream game architectures

failureForwardUrl与failureUrl

Comparative analysis of meta universe from the dimension of knowledge dissemination

C语言:表达式求值(整型提升、算术转换 ...)

Epidemic prevention registration applet

Planning and construction of industrial meta universe platform

0704、ansible----01

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

面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
随机推荐
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
Custom login failure handling
SAP pi / PO function operation status monitoring and inspection
Alibaba cloud architects interpret the four mainstream game architectures
Pyqt5 and communication
Rain produces hundreds of valleys, and all things grow
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
[codeforces - 208e] blood cousins
[COCI] Vje š TICA (subset DP)
Epidemic prevention registration applet
Computer network security experiment II DNS protocol vulnerability utilization experiment
构建元宇宙时代敏捷制造的九种能力
Mobius inversion
Odoo 服务器搭建备忘
Explanation of order and primitive root of number theory
工业元宇宙平台规划与建设
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
雨生百谷,万物生长
SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)
Prefix sum of integral function -- Du Jiao sieve