Symmetric Tree

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. 1
  2. / \
  3. 2 2
  4. / \ / \
  5. 3 4 4 3

But the following is not:

  1. 1
  2. / \
  3. 2 2
  4. \ \
  5. 3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

不管是递归还是非递归,找到关系就好做。

所谓对称,也就是:

1、left对应right

2、left->left对应right->right

3、left->right对应right->left

解法一:递归

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. bool isSymmetric(TreeNode *root) {
  13. if(!root)
  14. return true;
  15.  
  16. return isSymTree(root->left, root->right);
  17. }
  18. bool isSymTree(TreeNode* p, TreeNode* q)
  19. {
  20. if(!isSameNode(p, q))
  21. return false;
  22. if(!p && !q)
  23. return true;
  24. return isSymTree(p->left, q->right) && isSymTree(p->right, q->left);
  25. }
  26. bool isSameNode(TreeNode* p, TreeNode* q)
  27. {
  28. if(!p && !q)
  29. return true;
  30. if((!p && q) || (p && !q) || (p->val != q->val))
  31. return false;
  32. return true;
  33. }
  34. };

解法二:非递归

使用两个队列,对左右子树分别进行层次遍历。

进队时的对应元素比较即可。

  1. /**
  2. * Definition for binary tree
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. bool isSymmetric(TreeNode *root) {
  13. if(!root)
  14. return true;
  15.  
  16. if(!isSameNode(root->left, root->right))
  17. return false;
  18. if(!root->left && !root->right)
  19. return true;
  20.  
  21. queue<TreeNode*> lqueue;
  22. queue<TreeNode*> rqueue;
  23. lqueue.push(root->left);
  24. rqueue.push(root->right);
  25. while(!lqueue.empty() && !rqueue.empty())
  26. {
  27. TreeNode* lfront = lqueue.front();
  28. TreeNode* rfront = rqueue.front();
  29. lqueue.pop();
  30. rqueue.pop();
  31.  
  32. if(!isSameNode(lfront->left, rfront->right))
  33. return false;
  34. if(lfront->left && rfront->right)
  35. {
  36. lqueue.push(lfront->left);
  37. rqueue.push(rfront->right);
  38. }
  39.  
  40. if(!isSameNode(lfront->right, rfront->left))
  41. return false;
  42. if(lfront->right && rfront->left)
  43. {
  44. lqueue.push(lfront->right);
  45. rqueue.push(rfront->left);
  46. }
  47. }
  48. return true;
  49. }
  50. bool isSameNode(TreeNode* p, TreeNode* q)
  51. {
  52. if(!p && !q)
  53. return true;
  54. if((!p && q) || (p && !q) || (p->val != q->val))
  55. return false;
  56. return true;
  57. }
  58. };

【LeetCode】101. Symmetric Tree (2 solutions)的更多相关文章

  1. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  2. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【easy】101. Symmetric Tree

    判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...

  5. 【LeetCode】100. Same Tree (2 solutions)

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  6. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  7. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  9. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. iOS 画圆

    _demoView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:_de ...

  2. ROS知识(6)----基于Eclipse开发

    可以利用Eclipse集成开发环境进行ROS开发,从而提高研发效率.以色列巴尔伊兰大学的Mr. Roi Yehoshua开设了一门ROS课程,课程2( Lesson 2)讲解了如何利用Eclipse在 ...

  3. linuxmint - setup - 搜狗输入法

    安装好linuxmint18后,官网下载搜狗输入法安装包安装.安装成功后,发现缺失部分界面,包括输入候选框,软件设置,fcitx设置都不太正常. 解决: 安装:fcitx-ui-classic 另: ...

  4. mysql表前缀

    之前一直没明白,mysql有些规范里面,建议建表的时候添加前缀,它的意义究竟是为何.直到最近,我想学习一下Swift的网络请求,于是打算在新浪云新建个项目却发现新浪云免费用户最多只能建立5个项目.于是 ...

  5. 更改CentOS 6.3 yum源为国内 阿里云源

    将CentOS的 yum源 更换为 阿里云源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.b ...

  6. python接口自动化9-https请求(SSL)

    前言 本来最新的requests库V2.13.0是支持https请求的,但是一般写脚本时候,我们会用抓包工具fiddler,这时候会报:requests.exceptions.SSLError: [S ...

  7. N个富文本编辑器/基于Web的HTML编辑器

    转自:http://www.cnblogs.com/lingyuan/archive/2010/11/15/1877447.html 基于WEB的HTML 编辑器,WYSIWYG所见即所得的编辑器,或 ...

  8. U3D内存优化

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                                                U3D内存优化   读了Hog关于内存管理文章 ...

  9. OpenCV定制化创建角点检测子

    定制化创建角点检测子 目标 在这个教程中我们将涉及: 使用 OpenCV 函数 cornerEigenValsAndVecs 来计算像素对应的本征值和本征向量来确定其是否是角点. 使用OpenCV 函 ...

  10. 用IIS防止mdb数据库被下载

    如何防止mdb数据库被下载?本文讨论的是在服务器端禁止mdb格式数据库文件被下载,而不是在数据库中加入防下载表,将数据库名改为含#号的asp.asa等后缀格式. 下面以IIS6.0为例说明如何在服务器 ...