【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 ...
随机推荐
- HTML5 Canvas Text实例1
1.简单实例1 <canvas width="300" height="300" id="canvasOne" class=" ...
- linux 'more' command.
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...
- dotnet与各种数据库类型对应关系
Data Provider Parameter format sqlclient @parametername oledb ? mark odbc ? mark oracleclient : ...
- oracle系统包——dbms job用法(oracle定时任务)
用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务. 一.dbms_job涉及到的知识点1.创建job:variable jobno number;dbms_job.su ...
- ADO.NET复习——自己编写SqlHelper类
今天复习了一次ADO.NET基础,整理一下自己的认为的重点: 编写SqlHelper类,方便我们执行数据库语句,这时可以直接调用封装在SqlHelper类的方法.现在大多数公司面试的时候,给你的面试题 ...
- 解决java访问.netWebService的常见问题
到公司没多久,写了一个java调用.net写的webService结果期间用各种方法测试都没有完成,总是抛出异常,最后直接使用SOAP消息去进行调用才成功了,具体代码如下,仅供参考:import ja ...
- VBoxManage 命令行使用
原文地址:http://cnjun939.blog.163.com/blog/static/78144538201251474311135/ 由于最近需研究virtualbox,看好看到上面的网址有, ...
- javascript 中的nextSibling和previousSibling使用注意事项
JavaScript中的nextSibling和previousSibling和作用类似于jquery的next()和prev(),都是获取下一个/上一个同胞元素,如果下一个同级节点不存在,则此属性返 ...
- Yii 框架里数据库操作详解
增:1 第一种 $post=new Post; $post->title='sample post'; $post->content='content for the sample pos ...
- Dede 列表文章 自增
在{dede:arclist/}这个标签中有个[field:global.autoindex/],是从0开始自增,如果我们想自定义一个数值,比如自定义从2开始.那么就可以写成下面代码: [field: ...