【LeetCode】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 推断一棵树是否是镜像的的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- [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 ...
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ:Symmetric Tree(对称的树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode]Symmetric Tree @ Python
原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode Same Tree (判断相同树)
题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...
随机推荐
- 启动php-fpm时报错
[root@localhost ~]# /usr/local/php/sbin/php-fpm [06-Aug-2012 19:17:53] ALERT: [pool www] pm.min_spar ...
- 什么是NSAssert?
断言, 判断是否符合某个特定条件, 符合就继续运行程序, 反之就抛出异常, 后面为自定义错误提示, 也可以使用NSParameterAssert, 在调试上有着很大的方便 int a = 0; NSA ...
- onresize的定义方式
1.直接在html中定义如<body onresize="doResize()"/> 2.直接给onresize赋值给window和body的onresize赋值如wi ...
- C#的SerialPort串口程序设计总结
简介:微软的VS提供了SerialPort控件,也就是串行端口资源. 当然也可以添加引用 using System.IO.Ports; 通过实例化SerialPort对象就可以使用其属性和方法了. S ...
- Linux(Debian)上安装Redis教程
-- 第一步下载文件到该目录 cd /usr/local/src wget http:.tar.gz 解压 tar xzf redis.tar.gz -- 第二步编译安装 make make all ...
- jquery mobile navbar
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- Jquery的外部链接和编写样式
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></ ...
- Python学习6.1_函数参数及参数传递
大多数编程语言都绕不开一个名词,那就是--函数(function).而函数很重要的部分则是参数(arguments)的使用.Python的参数传递总体来说是根据位置,传递对应的参数.阐述如下: 1.位 ...
- Ubuntu完全教程,让你成为Ubuntu高手!
Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音.了解发音是有意义的,您不是第一个为此困惑的人,当然,也不会是最后一个:) 大多数的美国人读 ubun ...
- 周赛C题 LightOJ 1047 (DP)
C - C Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description Th ...