题目:

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);
}
};

最新文章

  1. Php compiler for .NET framework
  2. poi导出excel
  3. .NET面试题系列[11] - IEnumerable<T>的派生类
  4. 【原创】SQL分页查询存储过程
  5. Kettle6.0安装及问题总结-白痴教程
  6. VC++ 实现VC程序启动时最小化到任务栏(完美解决闪烁问题)
  7. 洛谷10月月赛Round.1| P3400 仓鼠窝[单调栈]
  8. 初识MVC,MVC里面的基本数据传递
  9. ASP.NET MVC Html.ActionLink使用说明
  10. Android中全屏或者取消标题栏
  11. mpi和cuda混合编程的正确编译
  12. 【转】使用ThinkPHP必须掌握的调试方法
  13. linux 计划任务(crontab)
  14. PE文件格式分析
  15. Linux_服务器_07_ 将用户设置为管理员
  16. Linux下编译器的安装
  17. p标签内容实现第二行缩进两个字体间距
  18. Power Designer 转C#实体类方法
  19. java Foreach与迭代器
  20. Delphi XE5 for Android (一)

热门文章

  1. cocos2d-x游戏开发 跑酷(四) 关联与物理世界
  2. Eclipse 每次打开workspace目录记录位置?
  3. 做SEO所要具备的四种能力
  4. XmlDocument.Load()加载xml文件时,提示分析 EntityName 时出错的问题。
  5. Android 网络编程 Socket Http
  6. SocketAsyncEventArgs使用解说
  7. hdu1159 LCS模板题
  8. JPush极光推送 Java调用服务器端API开发
  9. Windows下文件或文件夹不能删除时的解决办法
  10. C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象