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 ...
随机推荐
- 使用vue-router实现返回
在vue项目中使用vue-router做路由,做到返回页面的逻辑时,由于window.history.back满足不了返回的需要,故想通过 window.addEventListener('popst ...
- Python数据分析numpy库
1.简介 Numpy库是进行数据分析的基础库,panda库就是基于Numpy库的,在计算多维数组与大型数组方面使用最广,还提供多个函数操作起来效率也高 2.Numpy库的安装 linux(Ubuntu ...
- 定时器(setTimeout和setInterval)调用带参函数失效解决方法
方法1:用匿名函数包裹 function test(str){ alert(str); } var a = "abcde" setTimeout(function(){ ...
- 纯CSS写正方形自适应宽高,且左侧高与正方形高保持一致
先上图 虽然写法有点暴力,但是效果还是可以的,哈哈哈哈哈html:<div class="box"> <div class="le ...
- PeopleRank
PeopleRank:基于PageRank的理论,以每个微博账户的“关注”为链出链接,“粉丝”为链入链接的这种以人为核心的关系. PeopleRank假设条件:– 数量假设:如果一个用户节点接收到的其 ...
- 开天辟地-用visualstudio2010编写helloworld
安装好visual之后,创建新项目 向源文件添加helloworld.cpp 编写helloworld代码,编译运行即可 在运行时候出现一个错误,错误和解决方法如下:
- ACM-ICPC北京赛区(2017)网络赛_Minimum
题目9 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, -, a2^k ...
- asp.net core权限模块的快速构建
大部分系统都会有权限模块,别人家系统的权限怎么生成的我不知道,我只知道这样做是可以并且挺好的. 文章中只对asp.net core的部分代码进行说明 呃 记录~,mvc版本自行前往仓库查阅 代码中的一 ...
- vim与sublime,程序员的屠龙刀和倚天剑
对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持项目管理,界面难看-- ...
- You Are the One DP
You Are the One Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...