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

解题思路:

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

    public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode left,TreeNode right){
if(left==null||right==null)
return left==null&&right==null;
if(left.val!=right.val)
return false;
return(isSymmetric(left.left,right.right)&&isSymmetric(left.right,right.left));
}

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. iOS网络交互数据格式解析之json

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式.从ios5开 始,apple提供了对json的原生支持,但为了兼容以前的ios版本,我们仍然需要使用第三方库来解析常用 ...

  2. java.sql.Timestamp类型

    如果想向数据库中插入日期时间的话,可以用java.sql.Timestamp类 一个与 java.util.Date类有关的瘦包装器 (thin wrapper),它允许 JDBC API 将该类标识 ...

  3. python 类和__class__理解

    __class__可理解为对象所属的父类 class A: def __init__(self,url): self.url = url def out(self): return self.url ...

  4. TensorFlow笔记五:将cifar10数据文件复原成图片格式

    cifar10数据集(http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz)源格式是数据文件,因为训练需要转换成图片格式 转换代码: 注意文件路 ...

  5. ios 开发keywordIBInspectable

    这个keyword 能够让开发人员省去非常多事情 把部分界面设置 放在ui设计这里 能够让别人分担 特别是 像我如今所呆的小公司老变样 让他们也能够改 这里仅仅点出一下 有兴趣 自行搜索

  6. 拿wordpress站的一个小技巧

    记得09年时wp爆过一个重置管理口令的漏洞, 现在用法差不多, 也是我刚刚发现, 网上也没找到有讲述关于这个的. 前提:是在有注入点(注入点的话可以通过寻找插件漏洞获得.), 密码解不开, 无法out ...

  7. java查看工具jinfo-windows

    Generates configuration information. This command is experimental and unsupported. Synopsis jinfo [  ...

  8. linux设备驱动程序之并发和竞态(二)

    事实上这blog都是阅读ldd3时的一些总结,巩固自己的学习.也方便后期的使用.大家也能够直接阅读ldd3原文. 锁陷阱         所谓的锁陷阱就是防止死锁.         不明白的规则:   ...

  9. mysql报错1872: Slave failed to initialize relay log info structure from the repository

    ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 在一台主机上增加 ...

  10. C# 中三个关键字params,Ref,out

    一. using System; using System.Collection.Generic; using System.Text; namespace ParamsRefOut { class ...