当前位置:网站首页>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;
}();边栏推荐
- How to authorize subordinates?
- Unexpected exception ... code: Badrequest when downloading Xilinx 2018.2
- 节流的两种写法-最近看到的这种写法
- Autosar SWC在Simulink中Parameter的使用
- Check whether tensorflow supports GPU and test program
- ES中配置ext.dic文件不生效的原因
- Kingbasees v8r6 cluster maintenance case - data migration between clusters
- MFC中利用CDockablePane实现悬浮窗
- Markdown skill tree (4): link
- BeanPostProcessor 和 BeanFactoryPostProcessor
猜你喜欢

tf.count_nonzero

九州云助力内蒙古“东数西算”工程,驱动测绘行业智慧新生态

matlab simulink 电网扫频仿真和分析

Mmclassification installation and debugging
How to view software testing training? Do you need training?

Listen to textarea input through Keyup to change button style

ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
![[translation] E-Cloud. Large scale CDN using kubeedge](/img/ac/178c078589bb5bc16dbdc8f4ae9525.png)
[translation] E-Cloud. Large scale CDN using kubeedge

Use of parameter in Simulink for AUTOSAR SWC

Machine learning notes - time series prediction using machine learning
随机推荐
施努卡:视觉定位系统厂家 什么是视觉定位系统
【翻译】簇拥而出。构建现代应用程序的设计方法
from xx import*等价于from xx import *,不一定要加空格
jmeter 用beanshell导入自己jar包老是查找不到
Markdown skill tree (4): link
cv::Mat与Base64转换(含图片压缩解压等流程)
服装行业的CRM品牌供应商如何选型?
Schnuka: visual positioning system manufacturer what is a visual positioning system
Blue Bridge Cup - minimum frame
SAP UI5 初学 ( 一 )、简介
Schnuka: 3D visual inspection scheme 3D visual inspection application industry
Who is the main body of the waiting insurance record? Record in the local network security, right?
Swin Transformer理论讲解
Markdown skill tree (3): title
Appium automation test foundation ADB common commands (III)
Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
Markdown skill tree (9): tables
Spark RDD case: Statistics of daily new users
Schnuka: automatic tire grabbing installation, 3D visual positioning, automatic robot grabbing
Listen to textarea input through Keyup to change button style