题目: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. 四、分离T4引擎

         在前几篇文章中,我使用大量的篇幅来介绍T4在VisualStudio中如何使用.虽然在一定程度上可以提高我们的工作效率,但并没有实质上的改变.不过从另一方面来说,我们确实了解到了T4的强大. ...

  2. target,currentTarget,delegateTarget,srcElement

    第一种情况:就是IE9+和其他现代浏览器,支持addEventListener方法.其结果是: this总是等于currentTarget currentTarget总是事件监听者 target总是事 ...

  3. Tempdb对SQL Server性能的影响

    转载文章,原文地址:http://www.cnblogs.com/laodao1/archive/2010/04/15/1712395.html1.SQL Server系统数据库介绍 SQL Serv ...

  4. C++拾遗(三)关于复合类型

    数组相关 初始化只能在定义的时候使用,不能把数组赋给另一个数组. 初始化可以提供比元素数目少的初值,其它元素将被置为0. 字符char数组只有在以\0结尾时才是一个字符串.sizeof()返回数组的长 ...

  5. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

  6. java解析JSON (使用net.sf.json)

    例如JSON字符串str如下: {     "data": [         {             "basic_title": "运筹帷幄因 ...

  7. SecureCRT上使用公钥登陆Linux服务器

    SecureCRT部分配置 1.首先生成公钥. 打开SecureCRT(我的版本为7.0,估计其他版本基本相同)程序,点击菜单栏的“工具”->“创建公钥”.按照步骤执行.其中一步比较重要就是选择 ...

  8. php中字符串编码

    php中抓取网页拼接url的时候经常需要进行编码,这时候就用到两个函数 mb_detect_encoding — 检测字符的编码. mb_convert_encoding — 转换字符的编码 < ...

  9. Delphi-Delete 过程

    过程名称 Delete 所在单元 System 过程原型 procedure Delete ( var Source : string; StartChar : Integer; Count : In ...

  10. Linux下设置静态IP和获取动态IP的方法

    Linux下为机器设置静态IP地址: vim  /etc/sysconfig/network-scripts/ifcfg-eth0 修改这个文件内容如下形式: # Intel Corporation ...