当前位置:网站首页>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;
}();边栏推荐
- 国家安全局和CISA Kubernetes加固指南--1.1版的新内容
- What does VPS do? What are the famous brands? What is the difference with ECS?
- Markdown 技能树(7):分隔线及引用
- SYSTEMd management node exporter
- Appium automation test foundation ADB common commands (II)
- 通过keyup监听textarea输入更改按钮样式
- Utilisation d'IPv6 pour réaliser l'accès public au bureau distant
- 【翻译】e-Cloud。使用KubeEdge的大规模CDN
- Schnuka: automatic tire grabbing installation, 3D visual positioning, automatic robot grabbing
- uva10891
猜你喜欢

How to talk about salary correctly in software test interview?

Alternative writing of if else in a project

Markdown skill tree (5): picture

IMX6DL4.1.15支持EIM总线(下)——配置原理分析。

matlab simulink 电网扫频仿真和分析

Digital IC Design - UART

Use of parameter in Simulink for AUTOSAR SWC

mmclassification安装与调试

并发幂等性防抖

【FreeRTOS】中断机制
随机推荐
Suggestions on working methods and efficient work
感知健康生活 赋能无界连接 ——为OpenHarmony 3.1生态构建贡献芯海力量
HANA数据库License的查看申请及安装
【FreeRTOS】中断机制
SAP UI5 初学 ( 一 )、简介
Beanpostprocessor and beanfactorypostprocessor
关于开发web场景下如何解决手机访问web跨域问题
tf.compat.v1.global_variables
Es query syntax
Is virtual DOM really the fastest?
Using IPv6 to access remote desktop through public network
Listen to textarea input through Keyup to change button style
九州云助力内蒙古“东数西算”工程,驱动测绘行业智慧新生态
机器学习笔记 - 时间序列的混合模型
Swin Transformer理论讲解
Machine learning notes - time series prediction using machine learning
[software test] interface - Basic test process
软件测试面试如何正确谈论薪资?
Do you really understand "binder copy once"?
服装行业的CRM品牌供应商如何选型?