
[Easy] 101. Symmetric Tree
发布日期:2021-05-07 18:21:13
浏览次数:14
分类:精选文章
本文共 1017 字,大约阅读时间需要 3 分钟。
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1 / \ 2 2 / \ / \3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1 / \ 2 2 \ \ 3 3
Solution
8 ms 16.9 MB
class Solution { public: bool isSymmetric(TreeNode* root) { if(root == NULL) return true; queueq1,q2; TreeNode *left, *right; q1.push(root->left); q2.push(root->right); while(!q1.empty() && !q2.empty()) { left = q1.front();q1.pop(); right = q2.front();q2.pop(); if(left == NULL && right == NULL) continue; if(left == NULL || right == NULL) return false; if(left->val != right->val) return false; q1.push(left->left); q1.push(left->right); q2.push(right->right); q2.push(right->left); } return true; }};
使用queue,按顺序将节点入队:

发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月12日 09时26分08秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
「CF149D」括号涂色 区间DP好题
2021-05-07
树状数组 模板总结
2021-05-07
「NOI2015」程序自动分析 并查集题解
2021-05-07
[JSOI2008]Blue Mary的战役地图 Hash题解
2021-05-07
结构型设计在工作中的一些经验总结
2021-05-07
如何提升员工体验 助力企业业务增长?这个棘手的问题终于被解决了!
2021-05-07
2020 AI 产业图谱启动,勾勒中国 AI 技术与行业生态
2021-05-07
“编程能力差,90%输在了数学上!”CTO:多数程序员都是瞎努力!
2021-05-07
YbtOJ hash和hash表课堂过关 例1 字符串哈希【hash】
2021-05-07
前后端数据交互之表单
2021-05-07
剑指offer JZ21 栈的压入弹出序列
2021-05-07
Netty4服务端入门代码示例
2021-05-07
VL53L0x TOF激光测距的 stm32 HAL库驱动代码
2021-05-07
自定义标签(JSP2.0)简单标签
2021-05-07
MyBatis自定义类型转换器
2021-05-07
Python:函数 ----》装饰器函数
2021-05-07
Python:面向对象
2021-05-07
Spring源码:prepareBeanFactory(beanFactory);方法
2021-05-07
Spring源码:initApplicationEventMulticaster源码解析
2021-05-07
AcWing 786: 第k个数
2021-05-07