题目:Symmetric Tree

<span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树
* 思路:推断一棵树是否对称,1.有左子树就要有右子树
* 2.除根节点外对称节点值要同样
* 注意:对称后就是左子树的左节点和右子树的右节点比較
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
package javaTrain; public class Train8 {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
if(root.left == null && root.right == null) return true;
else if(root.left == null || root.right == null) return false;
return help(root.left,root.right);
}
private boolean help(TreeNode left,TreeNode right){
if(left == null && right == null) return true;
else if(left == null || right == null) return false;
if(left.val == right.val)
return help(left.left,right.right ) && help(left.right,right.left);
else return false;
}
}
</span></span>

【LeetCode】Symmetric Tree 推断一棵树是否是镜像的的更多相关文章

  1. [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树

    将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...

  2. [LeetCode] Equal Tree Partition 划分等价树

    Given a binary tree with n nodes, your task is to check if it's possible to partition the tree to tw ...

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

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

  4. LeetCode: Symmetric Tree 解题报告

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

  5. [LeetCode] Symmetric Tree 判断对称树

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

  6. LeetCode OJ:Symmetric Tree(对称的树)

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

  7. [leetcode]Symmetric Tree @ Python

    原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...

  8. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  9. LeetCode Same Tree (判断相同树)

    题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...

随机推荐

  1. SVN的使用(转发)

    http://my.oschina.net/joanfen/blog/194491?fromerr=LM5QY3YF

  2. Java并发编程之CAS

    CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术.简单来说,比较和替换是使用一个期望值和一个变量的当前值进行比较,如果当前变量的值与我们期望的值相等,就使用一个新值替 ...

  3. 【转】从头到尾彻底理解KMP

    很好,讲得很清晰,值得学习. 作者:July时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个月从早到晚不断改进. 1. 引言 本KMP原文最初写于2年多前的 ...

  4. 装饰者模式(Decorator)

    首先来看一个例子: 比如,饮料可以分为很多种类,而这里我取一个咖啡,那么这个咖啡呢,有多种形式的, 比如有加糖了的咖啡,有加奶的咖啡,也有加热了的咖啡,也有加了冰块的咖啡. 而各个顾客的选择却是不同的 ...

  5. wariging for you oh~

  6. ubuntu10.04版本下android源码的编译

    首先是网址:http://software.intel.com/en-us/blogs/2012/03/06/hands-on-notesbuild-android-x86-ics-4-virtual ...

  7. PHP图的绘制1

    最近在学习php图的绘制,写的代码放上来,供自己以后学习查看: <?php //*函数说明: //这个函数返回的是 // resource imagecreate ( int $x_size , ...

  8. 水仙花 AC 杭电

    水仙花数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. ASP.Net string 类的扩展方法 [转]

    string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<>     //累加 All<>        / ...

  10. XCode7打包上传报错

      在XCode7上传应用时,上传失败遇到两个错误,提示如下: ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundl ...