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

根开始,如果根节点的左右不对称,则false,否则,看根节点的左右子树是否对称。

代码:(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 left_right_Symmetric(TreeNode* pleft,TreeNode* pright)
{
if(pleft==NULL&&pright==NULL) return true;
if(pleft==NULL&&pright!=NULL)return false;
if(pleft!=NULL&&pright==NULL)return false;
if(pleft->val!=pright->val) return false;
return left_right_Symmetric(pleft->left,pright->right)&&left_right_Symmetric(pleft->right,pright->left);
}
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
if(root->left!=NULL&&root->right==NULL) return false;
if(root->left==NULL&&root->right!=NULL) return false;
if(root->left!=NULL&&root->right!=NULL&&root->left->val!=root->right->val)return false;
else return left_right_Symmetric(root->left,root->right);
}
};

错误代码:没有理解对称树的概念。(WA)

/**
* 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==NULL) return true;
if(root->left==NULL&&root->right!=NULL) return false;
else if(root->left!=NULL&&root->right==NULL)return false;
else if(root->left==NULL&&root->right==NULL) return true;
else if(root->left!=NULL&&root->right!=NULL&&root->left->val!=root->right->val) return false;
else if(root->left!=NULL&&root->right!=NULL&&root->left->val==root->right->val)
return isSymmetric(root->left)&&isSymmetric(root->right);
}
};



LeetCode_Symmetric Tree的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  3. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  9. Leetcode 笔记 101 - Symmetric Tree

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

随机推荐

  1. js prototype 理解

    简单理解:prototype对象是实现面向对象的一个重要机制.每个函数也是一个对象,它们对应的类就是 function,每个函数对象都具有一个子对象prototype.Prototype 表示了该函数 ...

  2. java读properties文件 乱码

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

  3. http常见的5个错误

    1. HTTP 500错误(内部服务器错误)对对HTTP 500错误的定义已经充分证明了这是一个最常见的HTTP错误. 一般来说,HTTP 500 错误就是web服务器发生内部错误时返回的信息. 例如 ...

  4. 在堆栈中,push为入栈操作,pop为出栈操作

    LinkedList提供以下方法:(ArrayList无此类方法) addFirst(); removeFirst(); addLast(); removeLast(); 在堆栈中,push为入栈操作 ...

  5. php -- 用文本来存储内容,file_put_contents,serialize,unserialize

    根据存储的内容来划分 字符串: file_put_contents :将一个字符串写入文件 语法:int file_put_contents ( string $filename , mixed $d ...

  6. mybatis总结(三)之多表查询

    上一节,已经把实体类和配置文件都写过了,这节课直接添加几个方法吧 在DeptMapper.xml文件中添加 <!-- 多表查询(1对多) ,通过部门编号,查询出部门所在的员工姓名,部门名,部门编 ...

  7. Android测试:从零开始2——local单元测试

    上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文 ...

  8. 递归删除资源树 Ztree

    前言 最近项目里有这么一个需求:现在有一个用Ztree编写的资源树,当删除资源树的某个节点时,则将此节点下面的所有节点全部删除,这里显然就用到了递归:若此节点被删除后无其它的兄弟节点了,我们还需要将其 ...

  9. golang中context包学习

    摘要 go语言中goroutine之间的关联关系,缺乏维护,在erlang中有专门的机制来保障新开仟程的生命周期, 在go语言中,只能通过channel + select来实现,但不够直观,感觉很绕. ...

  10. hdu 1300(dp)

    一个模式的dp. Pearls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...