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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

    1
/ \
2 2
\ \
3 3

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


题目标签:Tree

  这道题目给了我们一个二叉树,让我们判断这个二叉树是不是对称的。因为这里要比较两个点,所以需要另外一个function isSymmetric 来递归(recursively call)。所以我们来分析一下isSymmetric 这个function:代入的有2个点,那么有4中可能性:

  1- 如果两个点都是null,那么它们是相等的。返回true (这也是一种base case 表示结束了,走到树的最低端了,需要返回)

  2- 如果一个点是null,另外一个不是null,那么它们不相等,返回false ( base case, 表示一边已经走到底了,需要返回)

  3- 如果两个点都不是null,但是它们的值不相等, 返回false (判断条件,不相等,就返回)

  4- 如果两个点相等,那么我们需要继续往下走,来判断接下去的点:

    根据对称的特性,这里需要pass 两个情况返回function:(function 代入的是两个点,左边和右边)

      1- 把 左边点的左边,和右边点的右边 返回function;

      2- 把 左边点的右边,和右边点的左边 返回funciton。

      利用 && 来控制, 如果任务一个返回的值是fales,那么最终结果是false。(必须所有的两个对称点都相等)

Java Solution:

Runtime beats 23.77%

完成日期:07/01/2017

关键词:Tree

关键点:这里需要代入2个点 做比较和递归返回, 所以需要另外设一个funciton,它的input 是2个点;

    当function return 的时候, 需要return 两种情况, 根据对称性来,都靠外的2个点,和都靠里的2个点;

    利用 && 来控制每次返回的两种情况,如果对称的话,需要所有的返回都是true,任何一个false就说明 tree 不对称。

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public boolean isSymmetric(TreeNode root)
{
if(root == null)
return true; return isSymmetric(root.left, root.right);
} public boolean isSymmetric(TreeNode left, TreeNode right)
{
// if two nodes are null
if(left == null && right == null)
return true;
// is one node is null, another is another
if(left == null || right == null)
return false;
// if two nodes value are not same
if(left.val != right.val)
return false; return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); }
}

参考资料:

http://www.cnblogs.com/grandyang/p/4051715.html

    

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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对称树

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

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

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

  4. Leetcode 101 Symmetric Tree 二叉树

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

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

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

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

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

  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】Symmetric Tree(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...

  9. leetcode 101 Symmetric Tree ----- java

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

随机推荐

  1. 通过SDK和API获取阿里云RDS的监控数据

    阿里云的RDS自带的监控系统获取数据不怎么直观,想要通过API获取数据通过zabbix显示,因为网上资料缺乏和其他一些原因,获取API签名很困难,但使用阿里云的SDK可以完美避开获取签名的步骤. 阿里 ...

  2. temp-黄河农商行路径

    -------------------------------黄河农村商业银行------------------------------------ --1.--svn 地址:http://10.0 ...

  3. 如何定制 Calico 的 IP 池?- 每天5分钟玩转 Docker 容器技术(71)

    在前面的小节中,我们没有特别配置,calico 会为自动为网络分配 subnet,当然我们也可以定制. 首先定义一个 IP Pool,比如: cat << EOF | calicoctl ...

  4. Linux 常用命令之二

    整理以前学习Linux的笔记. 查找目录.查看当前所在路径.新建文件.查看文件内容.修改文件内容.压缩文件操作.搜索命令.管道命令.查看进程.终止进程.查看端口. 7,命令find--查找目录 fin ...

  5. JDFS:一款分布式文件管理系统,第四篇(流式云存储续篇)

    一 前言 本篇博客是JDFS系列博客的第四篇,从最初简单的上传.下载,到后来加入分布式功能,背后经历了大量的调试,尤其当实验的虚拟计算结点数目增加后,一些潜在的隐藏很深的bug就陆续爆发.在此之前笔者 ...

  6. snmp之GenericAddress

    GenericAddress 注册地址类型,而不是默认的,第一次调用解析(java.lang.String的)方法之前,设置系统属性ADDRESS_TYPES_PROPERTIES. 这个类涉及到了工 ...

  7. 读书笔记之宿舍共享wifi

    若有某方面侵权,请邮件1047697114@qq.com,一个工作日即可处理,谢谢   目录一.简单安装虚拟机二.简单设置,开热点! 我没试过那些wifi软件之类的,以下是个人测试的过程 一.简单安装 ...

  8. java递归的应用和实例

    使用计算机计算组合数: 1.使用组合数公式利用n!来计算 设计思想 (1)首先解决求n!的函数 (2)再结合组合数公式,求组合数 程序流程图 源程序代码 package Zuote; import j ...

  9. Python系列之正则表达式详解

    Python 正则表达式模块 (re) 简介 Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用 ...

  10. zoj3204 connect them 最小生成树 暴力

    Connect them Time Limit: 1 Second      Memory Limit:32768 KB You have n computers numbered from 1 to ...