给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称)。
例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的。
    1
   / \
  2   2
 / \ / \
3  4 4  3
但是下面这个 [1,2,2,null,3,null,3] 则不是:
    1
   / \
  2   2
   \   \
   3    3
说明:
如果你可以递归地和迭代地解决它就奖励你点数。
详见:https://leetcode.com/problems/symmetric-tree/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return helper(root.left,root.right);
}
private boolean helper(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;
}else if(left==null||right==null){
return false;
}else if(left.val!=right.val){
return false;
}else{
return helper(left.left,right.right)&&helper(left.right,right.left);
}
}
}

101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树的更多相关文章

  1. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

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

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

  3. 101. Symmetric Tree -- 判断树结构是否对称

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

  4. <LeetCode OJ> 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  5. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

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

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

  7. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

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

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

  9. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. html5--5-5 绘制填充矩形

    html5--5-5 绘制填充矩形 学习要点 掌握绘制矩形的方法:strkeRect()/fillRect() 掌握绘制路径的 beginPath()和closePath() 矩形的绘制方法 rect ...

  2. linux应用之tomcat的安装及配置(centos)

    CentOS 6.6下安装配置Tomcat环境 [日期:2015-08-25] 来源:Linux社区  作者:tae44 [字体:大 中 小]   实验系统:CentOS 6.6_x86_64 实验前 ...

  3. 在Angular.js中的H5页面调用Web api时跨域问题处理

    /// <summary> /// 被请求时 /// 在Angular.js中的H5页面调用Web api时跨域问题处理 /// </summary> /// <para ...

  4. WebApi发送HTML表单数据:文件上传与多部分MIME

    5.3 Sending HTML Form Data5.3 发送HTML表单数据(2) 本文引自:http://www.cnblogs.com/r01cn/archive/2012/12/20/282 ...

  5. H3C-交换机端口绑定

    1.端口和MAC地址绑定: (1)使用am命令: [switch]am user-bind mac-address 00e0-fc23-f8d3 interface Ehternet 0/1 (2)使 ...

  6. 无向图hash

    一个效果还行的 无向图hash判同构的方法 求出每个点向其它点的最短路,然后排序,然后按字符串拼接起来,再按每个点的字符串 排序后的rank 作为每一个点的初始hash值 然后每一轮,把每个点的相邻点 ...

  7. 单次目标检测器-SSD简介

    SSD 是使用 VGG19 网络作为特征提取器(和 Faster R-CNN 中使用的 CNN 一样)的单次检测器.我们在该网络之后添加自定义卷积层(蓝色),并使用卷积核(绿色)执行预测. 同时对类别 ...

  8. wampServer 设置

    设置端口 打开 C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf 文件 找到“Listen 80”和“ServerName localhost:80”,紧接 ...

  9. 关于layer.open() 弹框的使用

    在用 layer.open() 追加渲染HTML的时候,完全看不到效果,皆因layui框架需要在最后用 form.render() 方法来渲染表单才可以看到效果,写法如下: <script> ...

  10. 2.11-2.12 HBase的数据迁移常见方式

    一.importtsv 把hdfs中数据抽取到HBase表中: 1.准备数据 ##student.tsv [root@hadoop-senior datas]# cat student.tsv 100 ...