主要是检查该二叉树是否是自己的一个镜像(也就是以中心轴对称的)

 举例来说,下面显示的就是一个对称的二叉树

    1
/ \
2 2
/ \ / \
3 4 4 3 下面显示的就不是一个对称的二叉树了
    1
/ \
2 2
\ \
3 3 算法主要思想就是利用递归,左右子树同时遍历,若出现不一致的情况,则说明是不对称的
代码如下:
/**
* Definition for a binary tree node.
* 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 isMirrorTree(root.left,root.right);
}
} public boolean isMirrorTree(TreeNode leftNode,TreeNode rightNode){
if(leftNode!=null && rightNode!=null){
if(leftNode.val == rightNode.val){
if(isMirrorTree(leftNode.left,rightNode.right) && isMirrorTree(leftNode.right,rightNode.left)){
return true;
}else{
return false;
}
}else{
return false;
}
}else if(leftNode == null && rightNode == null){
return true;
}else{
return false;
}
}
}

  

 

LeetCode--判断二叉树是否对称的更多相关文章

  1. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

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

  2. 101. Symmetric Tree(判断二叉树是否对称)

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

  3. Leetcode:面试题28. 对称的二叉树

    Leetcode:面试题28. 对称的二叉树 Leetcode:面试题28. 对称的二叉树 Talk is cheap . Show me the code . /** * Definition fo ...

  4. [Leetcode 100]判断二叉树相同 Same Tree

    [题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...

  5. 剑指offer58:对称的二叉树。判断一颗二叉树是不是对称的,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的

    1 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 2 思路和方法 定义一种遍历算法,先遍历右子结点再遍历左子结点:如对称先序 ...

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

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

  7. 【剑指offer】判断二叉树是否为平衡二叉树

    2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...

  8. 剑指offer 判断树是不是对称的

    html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...

  9. 【easy】110. Balanced Binary Tree判断二叉树是否平衡

    判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...

  10. LeetCode 5 最长对称串

    LeetCode 5 最长对称串 最早时候做这道题的时候还是用Java写的,用的是字符串匹配的思路,一直Time Limit Exceeded.甚至还想过用KMP开优化子串查找. public cla ...

随机推荐

  1. VB中右键换行

    /r/n  能在邮件中进行换行, 在VB中使用 ASCII码的 chr(10).chr(13) 就能使VB发送邮件实现换行

  2. Android -- 双击退出

    实现android双击后退键退出当前APP功能 实现该功能基本思路是, 1, 监听后退键 , 比较两次后退间隔 , 低于两秒则出发退出 2, 退出当前APP 我选择在基类中BaseActivity 中 ...

  3. jquery ui 插件------------------------->sortable

    <!doctype html><html lang="en"><head>  <meta charset="utf-8" ...

  4. 完全卸载oracle

    今天在网上看到有位网友写的篇日志,感觉蛮好的,一般卸载oracle有4个地方需求注意:1)Services,2)software,3eventlog,4)path. 1.关闭 oracle 所有的服务 ...

  5. oracle 查看用户表数目,表大小,视图数目等

    查看当前用户的缺省表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * fr ...

  6. [转]MySQL数据库备份和还原的常用命令小结

    MySQL数据库备份和还原的常用命令小结,学习mysql的朋友可以参考下: 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword datab ...

  7. jq 图片裁剪

    1.html <div class="jcropbox" style="display: none"> <img src="&quo ...

  8. Jmeter软件测试2--http接口测试

    上次利用Jmeter进行了webservice接口的测试,本次利用Jmeter进行http接口的测试 1.新建线程组 2.新建配置文件 3.新建http请求 4.配置动态请求 4.查看测试结果

  9. poj 1273.PIG (最大流)

    网络流 关键是建图,思路在代码里 /* 最大流SAP 邻接表 思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧. 优化: 1.当前弧优化(重要). 1.每找到以条增广路回退到断点(常数优化). ...

  10. PHP 数组转JSON数据(convert array to JSON object);

    <?php header('Content-type: appliction/json; charset=shift-JIS'); $data =array(); class Test { pu ...