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

递归解法:

/**
* Definition for binary tree
* 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 isMirror(root.left,root.right);
}
}
public boolean isMirror(TreeNode left,TreeNode right)
{
if(left==null||right==null)//注:不能写成left==null&&right==null,报错
{
return left==right;
}
else
{
return (left.val==right.val)&&isMirror(left.left,right.right)&&isMirror(left.right,right.left);
}
}
}

Symmetric Tree的更多相关文章

  1. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  2. 【leetcode】Symmetric Tree

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

  3. 38. Same Tree && Symmetric Tree

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

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

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

  5. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  6. LeetCode: Symmetric Tree 解题报告

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

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

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

  8. 【LeetCode】101. Symmetric Tree (2 solutions)

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

  9. &lt;LeetCode OJ&gt; 101. Symmetric Tree

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

  10. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

随机推荐

  1. Protocol Framework - SNMP Tutorial

    30.4 Protocol Framework TCP/IP network management protocols2 divide the management problem into two ...

  2. easyUi 页面创建一个toolbar实例

    1.定义toolbar方法 pagination : true, pageSize : 10, pageList : [ 5, 10, 15, 20, 50 ], toolbar : toolbarF ...

  3. 使用 Eclipse 调试 Java 程序的 10 个技巧

    你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...

  4. Google map markers

    现已被屏蔽 http://mabp.kiev.ua/2010/01/12/google-map-markers/ Надо по немногу отходить от празднывания но ...

  5. [Unity] 3D数学基础 - 2D旋转矩阵

    2D矩阵的旋转: NewX = X * Cos(α) - Y * Sin(α) NewY = X * Sin(α) + Y * Cos(α) 一般在三角函数中使用的是弧度,我们可以通过下面的公式将角度 ...

  6. SQLPROMPT5.3对各种加密对象的解密测试

    SQLPROMPT5.3对各种加密对象的解密测试 测试环境: SQL2005个人开发者版 SP4 SQLPROMPT版本:5.3.8.2 视图 CREATE VIEW aa WITH ENCRYPTI ...

  7. 6Hibernate进阶----青软S2SH(笔记)

    关于关联关系的配置,用注解配置如下(这里引用的jar包是javax.persistence) // @ManyToOne(fetch=FetchType.LAZY) @ManyToOne(fetch= ...

  8. Tomcat端口被占用错误

    所报错误: 严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730013] ????????????? ...

  9. html 图像映射(一个图像多个连接)

    以前就见过那种导航地图,点击地图的不同省份分别跳到不同的连接,现在用html实现一下,简单的. 图像映射是指一个图像可以建立多个连接,就是在图像上面定义多个区域,每个区域连接到不同的地址. 效果如图: ...

  10. 【Bootstrap】Bootstrap-select多选下拉框实现

    目录 前言 需要引用的它们 核心选项 核心方法 实例应用 回到顶部 前言 项目中要实现多选,就想到用插件,选择了bootstrap-select. 附上官网api链接,http://silviomor ...