问题描述:

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

算法分析:和same tree比较像,都可以用递归方法来解决。

public class SymmeticTree
{
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 true;
}
else if ((left == null && right != null)
|| (right == null && left != null) || (left.val != right.val))
{
return false;
}
else
{
return isSymmetric(left.left, right.right)
&& isSymmetric(left.right, right.left);
}
}
}

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. Symmetric Tree 对称树

    判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...

  4. leetcode-Symmetric Tree 对称树

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

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

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

  6. [Leetcode] Symmetric tree 对称二叉树

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

  7. 第28题:leetcode101:Symmetric Tree对称的二叉树

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  8. 【LeetCode】Symmetric Tree(对称二叉树)

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

  9. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

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

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

随机推荐

  1. 【Android】Could not find XXX.apk!的解决方法

    昨天在Eclipse中导入一个Android工程后点击运行时出现了Could not find XXX.apk!的错误信息,具体错误提示如下:   到网上搜了好多方法,挨个尝试,最后都没解决但是,重启 ...

  2. VS2012编译FFmpeg 2.1.4

    这次的目标是完整编译FFmpeg包括汇编代码,并且尽量少修改源代码. 环境是WIN7 64位,VS2012, Intel.Parallel.Studio.XE.2013.SP1 一. 安装MinGW, ...

  3. 【转载】网络安全---Strurts2漏洞介绍

    Apache Struts2 作为世界上最流行的 Java Web 服务器框架之一,3 月 7 日带来了本年度第一个高危漏洞——CVE编号 CVE-2017-5638 .其原因是由于 Apache S ...

  4. Apache配置虚拟主机httpd-vhosts.conf

    一.配置虚拟主机需要3个文件  1.Apache/conf/httpd.conf 2.Apache/conf/extra/httpd-vhosts.conf (这个地版本的apache可能没有,可自己 ...

  5. Linux--vim编辑器和文件恢复

    第五章  Vim编辑器和恢复ext4下误删除的文件-Xmanager工具 本节所讲内容: 5.1  vim的使用 5.2  实战:恢复ext4文件系统下误删除的文件 5.3  实战:使用xmanage ...

  6. 对10进制16位长的主键的缩短处理 NULL

    # 对问题表去除旧有主键,新建自增主键:ALTER TABLE `question`CHANGE COLUMN `id` `id16` bigint(20) NULL COMMENT 'id_to_d ...

  7. mysql 约束条件 primary key 主键

    primary key字段的值不为空且唯一 约束:not null unique 存储引擎:innodb 对于innodb来说,一张表内必须有一个主键 单列做主键多列做主键(复合主键) 通常都是id字 ...

  8. hbase Java API 介绍及使用示例

    几个相关类与HBase数据模型之间的对应关系  java类 HBase数据模型 HBaseAdmin 数据库(DataBase) HBaseConfiguration HTable 表(Table) ...

  9. Selenium IDE编辑区域修改操作学习

    1.修改command.target.value,选择需要修改的步骤,然后点击下方,既可以直接进行修改. 2.添加新的操作步骤:直接在下方编辑区域的下方点击,然后输入或者选择操作类型,然后点击Targ ...

  10. redhat 5的中文包安装

    中文包文件名.在iso文件的/server/文件夹下fonts-chinese-3.02-9.6.el5.noarch.rpmfonts-ISO8859-2-75dpi-1.0-17.1.noarch ...