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

解题思路:

重载一个isSymmetric判断两棵树是否对称即可,JAVA实现如下:

  1. public boolean isSymmetric(TreeNode root) {
  2. if(root==null)
  3. return true;
  4. return isSymmetric(root.left,root.right);
  5. }
  6. public boolean isSymmetric(TreeNode left,TreeNode right){
  7. if(left==null||right==null)
  8. return left==null&&right==null;
  9. if(left.val!=right.val)
  10. return false;
  11. return(isSymmetric(left.left,right.right)&&isSymmetric(left.right,right.left));
  12. }

Java for LeetCode 101 Symmetric Tree的更多相关文章

  1. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  2. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  3. leetcode 101 Symmetric Tree ----- java

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

  4. Java [Leetcode 101]Symmetric Tree

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

  5. LeetCode 101. Symmetric Tree (对称树)

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

  6. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  7. LeetCode 101. Symmetric Tree

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

  8. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  9. Leetcode 101. Symmetric Tree(easy)

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

随机推荐

  1. 【spring boot】11.spring-data-jpa的详细介绍和复杂使用

    ==================================================================================================== ...

  2. xss---攻击

    xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在xss攻击中,通过插入恶意 ...

  3. linux下crontab使用笔记

    1. 安装     service crond status     yum install vixie-cron     yum install crontabs 2. 实例         每分钟 ...

  4. Swift 函数的定义与调用(Defining and Calling Functions)

    当你定义一个函数时,你能够定义一个或多个有名字和类型的值.作为函数的输入(称为參数.parameters).也能够定义某种类型的值作为函数运行结束的输出(称为返回类型). 每一个函数有个函数名,用来描 ...

  5. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  6. Solr Cloud的搭建使用

    Solr的安装下载http://archive.apache.org/dist/lucene/solr/6.4.0/或者直接去官网下载最新版本网页指导 https://cwiki.apache.org ...

  7. 《转》 在C++中使用TinyXML2解析xml

    读取和设置xml配置文件是最经常使用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,由于它的API接口和Java的十分类似.面向对象性非常好.       TinyX ...

  8. FMSC 使用理解

    看了非常长时间 FMSC资料 都说的模糊的. 事实上非常easy: fsmc就是为了扩展内存的,如我们在stm32芯片外加入一个sram芯片.那么我们仅仅须要把 sram芯片的地址线和数据线和stm3 ...

  9. Solidworks拖动装配体的时候物资动力有什么用

    Solidworks物资动力就是模拟真实的场景,你在拖动或旋转零件的时候会和周围的零件碰撞,有相互力的作用. 1 标准拖动   2 碰撞检查   3 选择物资动力(零件上面的黑白相间小圆表示物体重心) ...

  10. 如何修改mindmanager默认字体为微软雅黑

    mindmanager默认的字体是Verdana的,如何改为大家喜欢的其他字体呢?比如微软雅黑. 其实很简单,以我使用的是汉化版Mindmanager2012为例,只需要下面几个步骤   第1步:找到 ...