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.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

Solution 1: 递归,left对应right,left->left对应right->right,left->right对应right->left

 /**
* 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 isSymTree(root->left,root->right);
}
bool isSymTree(TreeNode *p,TreeNode *q){
if(!isSameNode(p,q))
return false;
else if(!p&&!q)
return true;
else
return isSymTree(p->left,q->right) && isSymTree(p->right,q->left);
}
bool isSameNode(TreeNode *p,TreeNode *q){
if(!p&&!q) //必需加上这个判断条件,否则若p、q为空下面的->val会运行时错误
return true;
else if((!p&&q)||(p&&!q)||(p->val!=q->val)) //利用||的短路效应避免运行时错误
return false;
return true;
}
};

Solution 2 :非递归,待续

【LeetCode】101 - Symmetric Tree的更多相关文章

  1. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

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

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

  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】199. Binary Tree Right Side View 解题报告(Python)

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

  6. Leetcode之101. Symmetric Tree Easy

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

  7. 【LeetCode】145. Binary Tree Postorder Traversal

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

  8. Leetcode 笔记 101 - Symmetric Tree

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

  9. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

随机推荐

  1. Data Flow ->> Raw File Source & Raw File Destination

    Raw File Source & Raw File Destination一般用在当有某个package在导入数据或者处理数据需要花费非常长的时间的情况下,可以通过把一些处理好的数据先存到r ...

  2. Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(二)

    然后是项目下的文件:完整的项目请看  上一篇 Maven+Spring+Hibernate+Shiro+Mysql简单的demo框架(一) 项目下的springmvc-servlet.xml配置文件: ...

  3. 8.cadence.CIS[原创]

    一.CIS数据库配置 ------ ---------------------------- --------------- ------------------ ---- 二.CIS放置元件 --- ...

  4. hihoCoder 1051补提交卡(贪心 枚举)

    http://hihocoder.com/problemset/problem/1051 既然要选择最长连续提交天数,那么提交卡必须连续使用才有可能得到最优解,这样贪心,然后从头到尾扫一遍求出最大值. ...

  5. leetcode:Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  6. 《OD大数据实战》Storm环境搭建

    一.环境搭建 1. 下载 http://www.apache.org/dyn/closer.lua/storm/apache-storm-0.9.6/apache-storm-0.9.6.tar.gz ...

  7. 利用XPath读取Xml文件

    之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...

  8. java线程安全理解

    java线程安全理解 如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码.如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的. ...

  9. CSS选择符类型

    一.标签选择符:针对某一类标签,可以以标签作为选择符 <style type="text/css"> p{color:#F00; font-size:36px;} &l ...

  10. android下身份验证方式调用webservice

    在企业开发领域,webservice还是经常被用到的服务体系,因为他对安全事务支持都比较好. 有时候,我们就需要在android下调用后端的webservice服务,因为在内部网络环境下,所有需要ba ...