当前位置:网站首页>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
边栏推荐
猜你喜欢

Juc并发编程07——公平锁真的公平吗(源码剖析)

SAP excel has completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.

杰理之更准确地确定异常地址【篇】
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

PHP笔记(一):开发环境配置

Windows安装redis并将redis设置成服务开机自启

云身份过于宽松,为攻击者打开了大门

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

Yarn核心参数配置

PHP notes (I): development environment configuration
随机推荐
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
杰理之用户如何最简单的处理事件【篇】
A concise course of fast Fourier transform FFT
art-template 模板引擎
解决VMware卸载后再安装出现的问题
Comparative analysis of meta universe from the dimension of knowledge dissemination
第一章 Oracle Database In-Memory 相关概念(IM-1.1)
1D / 1D dynamic programming learning summary
Epidemic prevention registration applet
Solving Lucas number and combination theorem
Example of data object mask used by SAP translate
Compile and debug mysql8 with clion under MacOS x
Introduction to sap pi / PO login and basic functions
通过流式数据集成实现数据价值(2)
Planning and construction of industrial meta universe platform
Cloud identity is too loose, opening the door for attackers
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
C language: expression evaluation (integer promotion, arithmetic conversion...)
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
[Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)