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. Linux系统PATH变量配置

    alias命令用于设置命令的别名,格式为“alias 别名=命令” 例如担心复制文件时误将文件被覆盖,可以执行alias cp=" cp -i",如此一来 每次复制命令都会询问用户 ...

  2. __init__ 和 self

    看代码 class A: def __init__(self, val): self.name = val def printName(self): print self.name a = A(&qu ...

  3. Mybatis Interceptor 拦截器原理 源码分析

    Mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变Mybatis的默认行为(诸如SQL重写之类的),由于插件会深入到Mybatis的核心,因此在编写自己的插件前最 ...

  4. GLSL基础

    GLSL基础 OpenGL Shading Language GLSL作为一种着色语言是纯粹的和GPU打交道的计算机语言.因为GPU是多线程并行处理器,所以GLSL直接面向SIMD模型的多线程计算.G ...

  5. AE 栅格图分级渲染

    ArcEngine对矢量数据进行风格化实在是得心应手,同样的对于栅格图像也能进行风格化!以前没接触过,今天正好需要,做出了栅格图像的渲染!下面实现的思路: 1.定义渲染的一系列接口 2.判断图像是否建 ...

  6. 超大整数运算算法——为RSA加密算法提供运算工具

    /* program: Large integer operations * Made by:  Daiyyr * date:  2013/07/09 * This software is licen ...

  7. Android 工程在4.0基础上混淆

    Android现在对安全方面要求比较高了,我今天要做的对apk进行混淆,用所有的第三方工具都不能反编译,作者的知识产权得到保障了,是不是碉堡了. 一,首先说明我这是在4.0基础上进行的. 先看看pro ...

  8. Java类的初始化过程及清理

    一.类的数据成员初始化 Java中类的数据成员初试化可能有两种形式. 在定义类成员变量的地方直接提供初始化值(这是C++中不允许的) 在构造器中初试化.(Java中不存在类似C++中的初始化列表) 两 ...

  9. JS获取系统的指定定年月日

    /** * 获取系统当前时间 */ function getNowYearMouth(){ var date=new Date; var nowYearMouth=date.getMonth()+1; ...

  10. ORA-12154 终极解决办法

    PLSQL连接Oracle数据库总出现12514分2中情况: 1.无法解析指定的连接标识符: 解决办法:我的电脑---属性---高级---环境变量   删除Path变量中C:\oracle\produ ...