题目

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

For example, this binary tree is symmetric:



But the following is not:



Note:

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

confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

分析

判断一棵二叉树是否为对称树;

仍然采用递归的思想,判断该树的左右子树是否对称;

若二叉树p与二叉树q对称,也就是说其根节点相同,p左子树应与q右子树对称,同理,p右子树应与q左子树对称;

AC代码

/**
* 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;
else
//判断左右子树是否对称
return isSymmetricTree(root->left, root->right);
} bool isSymmetricTree(TreeNode* p, TreeNode* q) {
//如果两个二叉树均为空,则返回true
if (!p && !q)
{
return true;
}
//如果两者其一为空树,则返回false
else if (!p || !q)
{
return false;
}
else{
if (p->val != q->val)
return false;
else
//p左子树应与q右子树对称,同理,p右子树应与q左子树对称
return isSymmetricTree(p->left, q->right) && isSymmetricTree(p->right, q->left);
}
}
};

LeetCode(101)Symmetric Tree的更多相关文章

  1. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

  2. LeetCode(25)-symmetric tree

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

  3. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. LeetCode(103) Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  5. LeetCode(124) Binary Tree Maximum Path Sum

    题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...

  6. LeetCode(26)-Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  7. LeetCode(101):对称二叉树

    Easy! 题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2, ...

  8. LeetCode(102) Binary Tree Level Order Traversal

    题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...

  9. LeetCode(100) Same Tree

    题目 Given two binary trees, write a function to check if they are equal or not. Two binary trees are ...

随机推荐

  1. scikit-learning教程(四)选择合适的估计量

    选择正确的估计 解决机器学习问题的最困难的部分通常是找到合适的工作量. 不同的估计器更适合于不同类型的数据和不同的问题. 下面的流程图旨在给用户一些关于如何处理关于哪些估计器尝试您的数据的问题的粗略指 ...

  2. fzu 2204 7 dp

    题目链接: fzu 2204 7 题目描述: 给出n个小球,每个小球只能涂黑色或者是白色,七个连续的不能是同种颜色,问有多少种涂色方法? 解题思路: 刚开始没有考虑到是环形的,WA的风生水起,怪我咯! ...

  3. 线段树(区间合并) HDOJ 3308 LCIS

    题目传送门 题意:线段树操作:1. 单点更新 2. 求区间的LCIS(longest consecutive increasing subsequence) 分析:注意是连续的子序列,就是简单的区间合 ...

  4. 关于能ping通服务器但ssh登陆不上的问题

    一般来说能ping通服务器说明网没问题 这是可以查看一下防火墙的设置和ip的屏蔽设置 /etc/init.d/iptables status  查看防火墙状态 vim /etc/hosts.allow ...

  5. java环境变量配置加maven配置

    1.安装JDK开发环境 下载网站:http://www.oracle.com/ 确定之后,单击“下一步”. 2.配置环境变量: 单击“计算机-属性-高级系统设置”,单击“环境变量”.在“系统变量”栏下 ...

  6. AJPFX关于读取properties 配置文件 返回属性值

    :Properties的概述        * Properties 类表示了一个持久的属性集.        * Properties 可保存在流中或从流中加载.        * 属性列表中每个键 ...

  7. AJPFX总结面向对象中成员变量和成员方法的定义

    //面向对象中成员变量和成员方法的定义格式:=========================================          成员变量定义在类中方法外,可以被该类中所有方法使用. ...

  8. CF622C Not Equal on a Segment

    题目链接: http://codeforces.com/problemset/problem/622/C 题目大意: 给定一个长度为n(n不超过200000)的序列,有m(m不超过200000)次询问 ...

  9. Scala 学习记录(一)

    1. 相对于java,scala的值修饰用val,变量修饰用var.值相当于java的final 修饰了. package demo object ScalaBase extends App { pr ...

  10. configure: error: MySQL library not found

    在CentOS系统中,安装zabbix进行configure时会遇到以下问题 ./configure --enable-server --enable-agent --with-mysql --wit ...