【101-Symmetric Tree(对称树)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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

  Note:

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

题目大意

  给定一棵树,推断它是否是对称的。

即树的左子树是否是其右子树的镜像。

解题思路

  使用递归进行求解。先推断左右子结点是否相等,不等就返回false。相等就将左子结点的左子树与右子结果的右子结点进行比較操作,同一时候将左子结点的左子树与右子结点的左子树进行比較,仅仅有两个同一时候为真是才返回true。否则返回false。

代码实现

树结点类

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;
} else {
return isSame(root.left, root.right);
}
} private boolean isSame(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
} if (left != null && right == null || left == null && right != null){
return false;
} else {
return left.val == right.val && isSame(left.left, right.right) && isSame(left.right, right.left);
}
}
}

评測结果

  点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333335

【LeetCode-面试算法经典-Java实现】【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 对称二叉树(Java & Python)

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

  4. [LeetCode]题解(python):101 Symmetric tree

    题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...

  5. Symmetric Tree 对称树

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

  6. 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

    [139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...

  7. 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】

    [053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...

  8. 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】

    [062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...

  9. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

随机推荐

  1. mysq-binlog

    Auth: JinDate: 2014-04-23参考: http://dev.mysql.com/doc/refman/5.1/en/replication-options-binary-log.h ...

  2. NHibernate 过滤器(第十五篇)

    NHibernate过滤器相当于定义一个非常类似于类和集合上使用的where子句.ISession 中默认是不启用过滤器的,必须通过ISession.EnableFilter()方法显式的启用. 该方 ...

  3. SQL Server 2012的内部原理和故障排除(50):Burgess_Liu的专栏

    http://blog.csdn.net/Burgess_Liu/article/category/1821435/2 http://www.cnblogs.com/fygh/archive/2012 ...

  4. material design动画

    这是一篇material design 文档动画部分的学习! Summary: Material Design动画交互 动画速度的3个原则 3种交互方式 如何设计有意义的动画 使人高兴的动画细节 1 ...

  5. WinCE6.0 2012年补丁下载地址

    Windows CE6.0 2012年补丁包WinCEPB60-121231-Product-Update-Rollup-Armv4I.msi下载地址:http://www.microsoft.com ...

  6. 封装log4cp p

    log4cpp 是参考 log4j 所写的 c++ 版本的写 log 的库.可以在这里下载   http://log4cpp.sourceforge.net/   我的使用方法是: 1,定义了一个 _ ...

  7. kibana-sentinl-监控报警

    kibana 安装 sentin 插件 ./bin/kibana-plugin install https://github.com/sirensolutions/sentinl/releases/d ...

  8. localstorge的缓存写法(超过一定时间自动清空)

    使用缓存: (设置缓存,尽量用大写,下划线的写法) const ls = { set: function (variable, value, ttl_ms) { var data = {value: ...

  9. 图片转为byte[]、String、图片之间的转换

    package com.horizon.action; import java.io.ByteArrayOutputStream; import java.io.File; import java.i ...

  10. java小游戏代码

    一. 需求分析 曾几何时,游戏是海洛因的代名词,让人与玩物丧志联系在一起.一度遭到社会反感和家长抵制.可是.随着互联网的发展,和游戏潜在优点被发现.游戏的价值開始逐渐被社会认可,人们開始接受.认识和了 ...