【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】
【101-Symmetric Tree(对称树)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
题目大意
给定一棵树,推断它是否是对称的。
即树的左子树是否是其右子树的镜像。
解题思路
使用递归进行求解。先推断左右子结点是否相等,不等就返回false。相等就将左子结点的左子树与右子结果的右子结点进行比較操作,同一时候将左子结点的左子树与右子结点的左子树进行比較,仅仅有两个同一时候为真是才返回true。否则返回false。
代码实现
树结点类
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;
} else {
return isSame(root.left, root.right);
}
}
private boolean isSame(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
} if (left != null && right == null || left == null && right != null){
return false;
} else {
return left.val == right.val && isSame(left.left, right.right) && isSame(left.right, right.left);
}
}
}
评測结果
点击图片,鼠标不释放。拖动一段位置。释放后在新的窗体中查看完整图片。
特别说明
欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333335】
【LeetCode-面试算法经典-Java实现】【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 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- [LeetCode]题解(python):101 Symmetric tree
题目来源 https://leetcode.com/problems/symmetric-tree/ Given a binary tree, check whether it is a mirror ...
- Symmetric Tree 对称树
判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...
- 【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】
[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of w ...
- 【LeetCode-面试算法经典-Java实现】【053-Maximum Subarray(最大子数组和)】
[053-Maximum Subarray(最大子数组和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Find the contiguous subarray w ...
- 【LeetCode-面试算法经典-Java实现】【062-Unique Paths(唯一路径)】
[062-Unique Paths(唯一路径)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 A robot is located at the top-left c ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
随机推荐
- 设计BBS
功能分析: 1 登陆功能(基于ajax,图片验证码) 2 注册功能(基于ajax,基于forms验证) 3 博客首页 4 个人站点 5 文章详情 6 点赞,点踩 7 评论 --根评论 --子评论 8 ...
- 【MySQL笔记】解除输入的安全模式,Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha ...
- Word中公式从单栏排版变为双栏排版后公式和编号错开了
如上图公式(2),把自己做的共识从通栏复制到期刊的双栏里就变成这样了(先复制过来参考文献,再复制正文,那么参考文献没事),原来一直搞不懂,今天把它显示所有标记发现多了个制表符(我原来以为是行标记),鼠 ...
- subline text 工具快捷键说明
编辑 ctrl + enter 光标当前行下一行新增一行,并且光标跳到下一行进行编辑 ctrl + shift +enter 光标当前行上一行新增一行,并且光标跳到上一行进行编辑 ...
- linux下activityMQ安装
>下载 到ActiveMQ官网,找到下载点. 目前, 官网为http://activemq.apache.org/ >启动 下载到本机,并解压 wget http://apache.f ...
- Selenium IDE 基础使用教程
Selenium IDE 基础使用教程 简介及安装 Selenium IDE 是一个易于使用的Firefox插件.它提供了一个图形用户界面,可进行脚本录制及导出.其记录的脚本可以被转换成多种编程语言( ...
- ElementUI使用问题记录:设置路由+iconfont图标+自定义表单验证
一.关于导航怎么设置路由 1.在el-menu这个标签的属性中添加 router ,官方文档的解释是:启用vue-router 这种模式 2.在el-menu-item标签中的index属性直接书写路 ...
- jquery 获取父窗口的元素、父窗口、子窗口
一.获取父窗口元素: $("#父窗口元素ID",window.parent.document):对应javascript版本为window.parent.document.getE ...
- Mapreduce 测试自带实例 wordcount
2.7.3版本的hadoop: jar程序所在目录:$HADOOP_HOME/shar/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.3.jar 1.本 ...
- Windows环境下Git配置及使用
Windows环境下Git配置及使用 一.安装包位置 Git下载地址https://git-scm.com/download/win TortoiseGit下载地址https://tortoisegi ...