当前位置:网站首页>101. 对称二叉树(递归与迭代方法)
101. 对称二叉树(递归与迭代方法)
2022-06-29 06:39:00 【吴贝贝97】
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
提示:
树中节点数目在范围 [1, 1000] 内
-100 <= Node.val <= 100
使用迭代可以达到100%的效率,尽量多用迭代
/**
* 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 compare(root->left, root->right);
// }
// bool compare(TreeNode* left, TreeNode* right){
// if(left == nullptr && right == nullptr){
// return true;
// }else if (left != nullptr && right == nullptr){
// return false;
// }else if (left == nullptr && right != nullptr){
// return false;
// }else if (left->val != right->val){
// return false;
// }
// bool outside = compare(left->left, right->right);
// bool inside = compare(left->right, right->left);
// bool isSame = outside && inside;
// return isSame;
// }
// };
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL)
return true;
//使用两个队列进行操作
queue<TreeNode*> left;
queue<TreeNode*> right;
left.push(root->left);
right.push(root->right);
while(!left.empty() && !right.empty()){
TreeNode* left_node = left.front();
TreeNode* right_node = right.front();
left.pop();
right.pop();
if (left_node == NULL && right_node == NULL)
continue;
if ((left_node == NULL && right_node != NULL) || (left_node != NULL && right_node == NULL))
return false;
if (left_node->val != right_node->val)
return false;
// 注意进对顺序
left.push(left_node->left);
left.push(left_node->right);
right.push(right_node->right);
right.push(right_node->left);
}
return true;
}
};
static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();边栏推荐
- 【翻译】e-Cloud。使用KubeEdge的大规模CDN
- 项目中 if else 的代替写法
- golang 修改 结构体切片的值
- tf.count_nonzero
- Es query syntax
- [popular science materials] materials from scientific spirit to scientific knowledge
- Blue Bridge Cup - minimum frame
- What tools do testers need to know
- Is virtual DOM really the fastest?
- Reflection modification final
猜你喜欢

Using IPv6 to access remote desktop through public network

Swin Transformer理论讲解

Appium自动化测试基础 — ADB常用命令(二)

Autosar SWC在Simulink中Parameter的使用
如何看待软件测试培训?你需要培训吗?

Deploy Prometheus server service system management

KingbaseES V8R6集群维护案例之--单实例数据迁移到集群案例

tf. count_ nonzero

Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
How to view software testing training? Do you need training?
随机推荐
Markdown skill tree (2): paragraph and emphasis
Schnuka: visual positioning system manufacturer what is a visual positioning system
What does VPS do? What are the famous brands? What is the difference with ECS?
golang 修改 结构体切片的值
Vibration signal generation and processing based on MATLAB Doppler effect
tf.count_nonzero
Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
反射修改final
Markdown 技能树(9):表格
ES中配置ext.dic文件不生效的原因
[qnx hypervisor 2.2 user manual]6.2.1 communication between guests
ES 查询语法
Relevance - correlation analysis
服裝行業的CRM品牌供應商如何選型?
1031 Hello World for U
Livedata source code appreciation - basic use
Digital IC Design - UART
Imx6dl4.1.15 supports EIM bus (Part 2) - configuration principle analysis.
Markdown skill tree (3): title
[translation] E-Cloud. Large scale CDN using kubeedge