LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题目标签:Tree
这道题目给了我们一个二叉树,让我们判断这个二叉树是不是对称的。因为这里要比较两个点,所以需要另外一个function isSymmetric 来递归(recursively call)。所以我们来分析一下isSymmetric 这个function:代入的有2个点,那么有4中可能性:
1- 如果两个点都是null,那么它们是相等的。返回true (这也是一种base case 表示结束了,走到树的最低端了,需要返回)
2- 如果一个点是null,另外一个不是null,那么它们不相等,返回false ( base case, 表示一边已经走到底了,需要返回)
3- 如果两个点都不是null,但是它们的值不相等, 返回false (判断条件,不相等,就返回)
4- 如果两个点相等,那么我们需要继续往下走,来判断接下去的点:
根据对称的特性,这里需要pass 两个情况返回function:(function 代入的是两个点,左边和右边)
1- 把 左边点的左边,和右边点的右边 返回function;
2- 把 左边点的右边,和右边点的左边 返回funciton。
利用 && 来控制, 如果任务一个返回的值是fales,那么最终结果是false。(必须所有的两个对称点都相等)
Java Solution:
Runtime beats 23.77%
完成日期:07/01/2017
关键词:Tree
关键点:这里需要代入2个点 做比较和递归返回, 所以需要另外设一个funciton,它的input 是2个点;
当function return 的时候, 需要return 两种情况, 根据对称性来,都靠外的2个点,和都靠里的2个点;
利用 && 来控制每次返回的两种情况,如果对称的话,需要所有的返回都是true,任何一个false就说明 tree 不对称。
/**
* 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; return isSymmetric(root.left, root.right);
} public boolean isSymmetric(TreeNode left, TreeNode right)
{
// if two nodes are null
if(left == null && right == null)
return true;
// is one node is null, another is another
if(left == null || right == null)
return false;
// if two nodes value are not same
if(left.val != right.val)
return false; return isSymmetric(left.left, right.right) && isSymmetric(left.right, right.left); }
}
参考资料:
http://www.cnblogs.com/grandyang/p/4051715.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 101. Symmetric Tree (对称树)的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- thymeleaf模板引擎调用java类中的方法(附源码)
前言 <Docker+SpringBoot+Mybatis+thymeleaf的Java博客系统开源啦> 由于开源了项目的缘故,很多使用了My Blog项目的朋友遇到问题也都会联系我去解决 ...
- MyeclipseJRE版本设置
1.首先添加JDK版本 Window——Preferences——Java——Install JREs——Add——Stand VM——浏览JDK安装版本完成即可(一定是JDK中JRE的安装目录如:D ...
- [js学习笔记] 原型链理解
js中只有对象,包括对象,函数,常量等. prototype 只有函数里有这个属性. 对象里可以设置这个属性,但是没这个属性的意义 prototype指向一个对象,可以添加想要的方法. 该对象里有一个 ...
- Https系列之一:https的简单介绍及SSL证书的生成
Https系列会在下面几篇文章中分别作介绍: 一:https的简单介绍及SSL证书的生成二:https的SSL证书在服务器端的部署,基于tomcat,spring boot三:让服务器同时支持http ...
- SVG轨迹回放实践
最近做了埋点方案XTracker的轨迹回放功能,大致效果就是,在指定几个顺序的点之间形成轨迹,来模拟用户在页面上的先后行为(比如一个用户先点了啥,后点了啥).效果图如下: 在这篇文章中,我们来聊聊轨迹 ...
- hibernate学习手记(2)
1.javax.persistence.TransactionRequiredException: no transaction is in progress 出现该问题是我没有开启事务,我是在保存之 ...
- CentOS 7安装squid代理服务器
Squid,一个高性能的代理缓存服务器,支持FTP.gopher.HTTP协议. Squid,一个缓存Internet 数据的软件,其接收用户的下载申请(作为代理服务器),并自动处理所下载的数据,并返 ...
- [js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标
有了前面的canvas基础之后,现在开始就精彩了,后面写的canvas教程都是属于综合应用,前面已经写了常用的canvas基础知识,参考链接如下: [js高手之路] html5 canvas系列教程 ...
- TOP命令详解(负载情况)
简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...
- css常见布局方式
CSS常见布局方式 以下总结一下CSS中常见的布局方式.本人才疏学浅,如有错误,请留言指出. 如需转载,请注明出处:CSS常见布局方式 目录: 使用BFC隐藏属性 float + margin abs ...