[LeetCode101]Symmetric Tree
2024-09-20 15:13:54
题目:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
分类:Tree BFS DFS
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool isSymmetric(TreeNode *root) {
if (!root) return true;
return helper(root->left, root->right);
} bool helper(TreeNode* p, TreeNode* q) {
if (!p && !q) {
return true;
} else if (!p || !q) {
return false;
} if (p->val != q->val) {
return false;
} return helper(p->left,q->right) && helper(p->right, q->left);
}
};
最新文章
- Php compiler for .NET framework
- poi导出excel
- .NET面试题系列[11] - IEnumerable<;T>;的派生类
- 【原创】SQL分页查询存储过程
- Kettle6.0安装及问题总结-白痴教程
- VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
- 洛谷10月月赛Round.1| P3400 仓鼠窝[单调栈]
- 初识MVC,MVC里面的基本数据传递
- ASP.NET MVC Html.ActionLink使用说明
- Android中全屏或者取消标题栏
- mpi和cuda混合编程的正确编译
- 【转】使用ThinkPHP必须掌握的调试方法
- linux 计划任务(crontab)
- PE文件格式分析
- Linux_服务器_07_ 将用户设置为管理员
- Linux下编译器的安装
- p标签内容实现第二行缩进两个字体间距
- Power Designer 转C#实体类方法
- java Foreach与迭代器
- Delphi XE5 for Android (一)
热门文章
- cocos2d-x游戏开发 跑酷(四) 关联与物理世界
- Eclipse 每次打开workspace目录记录位置?
- 做SEO所要具备的四种能力
- XmlDocument.Load()加载xml文件时,提示分析 EntityName 时出错的问题。
- Android 网络编程 Socket Http
- SocketAsyncEventArgs使用解说
- hdu1159 LCS模板题
- JPush极光推送 Java调用服务器端API开发
- Windows下文件或文件夹不能删除时的解决办法
- C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象